zhber
有好多做过的题没写下来,如果我还能记得就补吧
B. Complete The Graph
time limit per test 4 seconds
memory limit per test 256 megabytes
input standard input
output standard output

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?

Input

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.

Output

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.

Examples
Input
5 5 13 0 4
0 1 5
2 1 2
3 2 3
1 4 0
4 3 4
Output
YES
0 1 5
2 1 2
3 2 3
1 4 8
4 3 4
Input
2 1 123456789 0 1
0 1 0
Output
YES
0 1 123456789
Input
2 1 999999999 1 0
0 1 1000000000
Output
NO
Note

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".

 

题意是n个点m条无向边,边权都为正,有些边边权定了,有些没定。现在问能不能确定那些还未确定的边权,使得s到t的最短路恰好为L

这题脑洞好大,而且对于spfa选手很不友好

首先把确定边权的边跑个最短路,如果dist<L一定无解(因为加边不会增加最短路),如果dist==L输出方案

再把未确定的边全部改成1,扔进去跑最短路,如果dist>L或者不连通一定无解(因为增加边权不会减少最短路),如果dist==L输出方案

然后是这样的骚操作:

每次取一条当前路径上的未确定边权的边,这个很好做,在跑最短路的时候随便记一记

然后把它的边权v改成v+(L-dist),标记它为已确定的边

换句话说现在这条路径的长度就是L了,但是它现在不一定还是最短路

然后重复跑最短路+修改边,直到最短路为L,或者路径上已经没有可以修改的边

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<queue>
  8 #include<deque>
  9 #include<set>
 10 #include<map>
 11 #include<ctime>
 12 #define LL long long
 13 #define inf 0x7ffffff
 14 #define pa pair<LL,int>
 15 #define mkp(a,b) make_pair(a,b)
 16 using namespace std;
 17 inline LL read()
 18 {
 19     LL x=0,f=1;char ch=getchar();
 20     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 21     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
 22     return x*f;
 23 }
 24 int n,m,s,t,cnt=1;
 25 LL L;
 26 priority_queue<pa,vector<pa>,greater<pa> >q;
 27 struct ed{int x,y,id;LL z;}d[10010];
 28 struct edge{
 29     int to,next;
 30     LL v;
 31     bool mrk;
 32 }e[20010];
 33 LL dis[1010];
 34 bool vis[1010];
 35 int sv[1010];
 36 int head[1010];
 37 inline void ins(int u,int v,LL w,int mrk)
 38 {
 39     //printf("ins %d %d %lld %d\n",u,v,w,mrk);
 40     e[++cnt].to=v;
 41     e[cnt].v=w;
 42     e[cnt].mrk=mrk;
 43     e[cnt].next=head[u];
 44     head[u]=cnt;
 45 }
 46 inline void insert(int u,int v,LL w,bool mrk)
 47 {
 48     ins(u,v,w,mrk);
 49     ins(v,u,w,mrk);
 50 }
 51 inline void dijkstra()
 52 {
 53     for (int i=1;i<=n;i++)dis[i]=-1,sv[i]=0,vis[i]=0;
 54     dis[s]=0;q.push(mkp(0,s));
 55     while (!q.empty())
 56     {
 57         int now=q.top().second;q.pop();
 58         if (vis[now])continue;vis[now]=1;
 59         for(int i=head[now];i;i=e[i].next)
 60         if(dis[e[i].to]==-1||dis[now]+e[i].v<dis[e[i].to])
 61         {
 62             dis[e[i].to]=dis[now]+e[i].v;
 63             sv[e[i].to]=sv[now];
 64             if (e[i].mrk)sv[e[i].to]=i;
 65             q.push(mkp(dis[e[i].to],e[i].to));
 66         }
 67     }
 68 }
 69 int main()
 70 {
 71     n=read();m=read();L=read();s=read()+1;t=read()+1;
 72     for (int i=1;i<=m;i++)
 73     {
 74         d[i].x=read()+1;d[i].y=read()+1;d[i].z=read();
 75         if (d[i].z)insert(d[i].x,d[i].y,d[i].z,0),d[i].id=cnt;
 76     }
 77     dijkstra();
 78     if (dis[t]<L&&dis[t]!=-1){puts("NO");return 0;}
 79     if (dis[t]==L)
 80     {
 81         puts("YES");
 82         for (int i=1;i<=m;i++)
 83             if(d[i].z)printf("%d %d %d\n",d[i].x-1,d[i].y-1,d[i].z);
 84             else printf("%d %d 2000000000\n",d[i].x-1,d[i].y-1);
 85         return 0;
 86     }
 87     for (int i=1;i<=m;i++)
 88     if (!d[i].z)insert(d[i].x,d[i].y,1,1),d[i].id=cnt;
 89     dijkstra();
 90     if (dis[t]==-1){puts("NO");return 0;}
 91     if (dis[t]>L){puts("NO");return 0;}
 92     if (dis[t]==L)
 93     {
 94         puts("YES");
 95         for (int i=1;i<=m;i++)
 96             if(d[i].z)printf("%d %d %d\n",d[i].x-1,d[i].y-1,d[i].z);
 97             else printf("%d %d 1\n",d[i].x-1,d[i].y-1);
 98         return 0;
 99     }
100     int tot=0;
101     while (dis[t]<L)
102     {
103         //printf("%d %d %d\n",e[sv[t]].to,sv[t],dis[t]);
104         e[sv[t]].v+=L-dis[t];
105         e[sv[t]^1].v+=L-dis[t];
106         e[sv[t]].mrk=e[sv[t]^1].mrk=0;
107         dijkstra();
108     }
109     if (dis[t]!=L)puts("NO");
110     else
111     {
112         puts("YES");
113         for (int i=1;i<=m;i++)
114         {
115             printf("%d %d %lld\n",d[i].x-1,d[i].y-1,d[i].z?d[i].z:e[d[i].id].v);
116         }
117     }
118 }
cf 715B

 

posted on 2017-07-11 21:07  zhber  阅读(1358)  评论(0编辑  收藏  举报