codeforces 715B:Complete The Graph
Posted ljh2000
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeforces 715B:Complete The Graph相关的知识,希望对你有一定的参考价值。
Description
ZS the Coder has drawn an undirected graph of n vertices numbered from 0 to n - 1 and m edges between them. Each edge of the graph is weighted, each weight is a positive integer.
The next day, ZS the Coder realized that some of the weights were erased! So he wants to reassign positive integer weight to each of the edges which weights were erased, so that the length of the shortest path between vertices s and t in the resulting graph is exactly L. Can you help him?
The first line contains five integers n, m, L, s, t (2 ≤ n ≤ 1000, 1 ≤ m ≤ 10 000, 1 ≤ L ≤ 109, 0 ≤ s, t ≤ n - 1, s ≠ t) — the number of vertices, number of edges, the desired length of shortest path, starting vertex and ending vertex respectively.
Then, m lines describing the edges of the graph follow. i-th of them contains three integers, ui, vi, wi (0 ≤ ui, vi ≤ n - 1, ui ≠ vi, 0 ≤ wi ≤ 109). ui and vi denote the endpoints of the edge and wi denotes its weight. If wi is equal to 0 then the weight of the corresponding edge was erased.
It is guaranteed that there is at most one edge between any pair of vertices.
Print "NO" (without quotes) in the only line if it‘s not possible to assign the weights in a required way.
Otherwise, print "YES" in the first line. Next m lines should contain the edges of the resulting graph, with weights assigned to edges which weights were erased. i-th of them should contain three integers ui, vi and wi, denoting an edge between vertices ui and vi of weight wi. The edges of the new graph must coincide with the ones in the graph from the input. The weights that were not erased must remain unchanged whereas the new weights can be any positive integer not exceeding 1018.
The order of the edges in the output doesn‘t matter. The length of the shortest path between s and t must be equal to L.
If there are multiple solutions, print any of them.
5 5 13 0 4
0 1 5
2 1 2
3 2 3
1 4 0
4 3 4
YES
0 1 5
2 1 2
3 2 3
1 4 8
4 3 4
2 1 123456789 0 1
0 1 0
YES
0 1 123456789
2 1 999999999 1 0
0 1 1000000000
NO
Here‘s how the graph in the first sample case looks like :
In the first sample case, there is only one missing edge weight. Placing the weight of 8 gives a shortest path from 0 to 4 of length 13.
In the second sample case, there is only a single edge. Clearly, the only way is to replace the missing weight with 123456789.
In the last sample case, there is no weights to assign but the length of the shortest path doesn‘t match the required value, so the answer is "NO".
正解:dijkstra
解题报告:
其实这道题也很简单,考场上没想出来真是我傻...
首先那些可以修改的边最小边权为1,则全部改为1,那么如果此时最短路小于等于L,那么至少是有解的。否则无解。
然后依次修改当前还差的边权,直到合法,反正CF机子快,丝毫不虚。
然而我WA了一个晚上,因为我的inf设大了,一加就炸,调了很久才发现...
1 //It is made by jump~ 2 #include <iostream> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cstdio> 6 #include <cmath> 7 #include <algorithm> 8 #include <ctime> 9 #include <vector> 10 #include <queue> 11 #include <map> 12 #include <set> 13 using namespace std; 14 typedef long long LL; 15 const int MAXN = 1011; 16 const int MAXM = 20011; 17 const int MOD = 2000000; 18 int n,m,L,s,t,ecnt; 19 int first[MAXN],to[MAXM],next[MAXM],w[MAXM],dis[MAXN]; 20 int tag[MAXM];//!!!!! 21 int dui[MAXN*10],head,tail; 22 bool in[MAXN],ok; 23 24 inline int getint() 25 { 26 int w=0,q=0; char c=getchar(); 27 while((c<‘0‘ || c>‘9‘) && c!=‘-‘) c=getchar(); if(c==‘-‘) q=1,c=getchar(); 28 while (c>=‘0‘ && c<=‘9‘) w=w*10+c-‘0‘, c=getchar(); return q ? -w : w; 29 } 30 31 inline void SPFA(){ 32 //memset(dis,63,sizeof(dis)); 33 for(int i=1;i<=n;i++) dis[i]=1000000001;//不能设太大!!!不然会炸,WA了好久!!! 34 dis[s]=0; head=tail=0; dui[++tail]=s; memset(in,0,sizeof(in)); in[s]=1; 35 while(head!=tail) { 36 head%=n; head++; int u=dui[head]; in[u]=0; 37 for(int i=first[u];i>0;i=next[i]) { 38 int v=to[i]; 39 if(dis[v]>dis[u]+w[i]) { 40 dis[v]=dis[u]+w[i]; 41 if(!in[v]) { tail%=n; tail++; dui[tail]=v; in[v]=1; } 42 } 43 } 44 } 45 } 46 47 inline void work(){ 48 //memset(first,-1,sizeof(first)); 49 n=getint(); m=getint(); L=getint(); s=getint()+1; t=getint()+1; 50 int x,y,z;ecnt=1; 51 for(int i=1;i<=m;i++) { 52 x=getint()+1; y=getint()+1; z=getint(); 53 next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; w[ecnt]=z; if(z==0) tag[ecnt]=1,w[ecnt]=1; 54 next[++ecnt]=first[y]; first[y]=ecnt; to[ecnt]=x; w[ecnt]=z; if(z==0) tag[ecnt]=1,w[ecnt]=1; 55 } 56 SPFA(); if(dis[t]>L) { printf("NO"); return ; } 57 ok=false; 58 for(int i=1;i<=n;i++) { 59 for(int j=first[i];j>0;j=next[j]) { 60 if(j & 1) 61 if(tag[j]) { 62 if(dis[t]==L){ ok=true ; break;} 63 if(dis[t]<L) { 64 w[j]=w[j^1]=L-dis[t]+1; 65 SPFA(); 66 } 67 } 68 } 69 if(ok) break; 70 } 71 if(!ok && dis[t]!=L) { printf("NO"); } 72 else{ 73 printf("YES\n"); 74 for(int i=1;i<=n;i++) 75 for(int j=first[i];j>0;j=next[j]) 76 if(j&1) 77 printf("%d %d %d\n",to[j]-1,to[j^1]-1,w[j]); 78 } 79 } 80 81 int main() 82 { 83 work(); 84 return 0; 85 }
以上是关于codeforces 715B:Complete The Graph的主要内容,如果未能解决你的问题,请参考以下文章