SGU 185 Two shortest

Two shortest

Time Limit: 500ms
Memory Limit: 4096KB
This problem will be judged on SGU. Original ID: 185
64-bit integer IO format: %I64d      Java class name: Solution
 
Yesterday Vasya and Petya quarreled badly, and now they don't want to see each other on their way to school. The problem is that they live in one and the same house, leave the house at the same time and go at the same speed by the shortest road. Neither of them wants to change their principles, that is why they want to find two separate shortest routes, which won't make them go along one road, but still they can meet at any junction. They ask you to help them. They number all the junctions with numbers from 1 to N (home and school are also considered as junctions). So their house has the number 1 and the school has the number N, each road connects two junctions exactly, and there cannot be several roads between any two junctions.
 

Input

The first line contains two integer numbers N and M (2<=N<=400), where M is the number of roads Petya and Vasya noticed. Each of the following M lines contains 3 integers: X, Y and L (1<=X, Y<=N, 1<=L<=10000), where X and Y - numbers of junctions, connected by the road and L is the length of the road.
 

Output

Write to the first line numbers of the junctions in the way they passed them on the first route. Write to the second line numbers of the junctions in the way they passed them on the second route. If it is impossible to help guys, then output "No solution".
 

Sample Input


Sample test(s)

Input
 
 
6 8
1 2 1
3 2 1
3 4 1
1 3 2
4 2 2
4 5 1
5 6 1
4 6 2
 
 

Output
 
 
1 3 4 5 6
1 2 4 6
 
 

Source

 
解题:妈拉个巴子,写了三遍才过。。
 
  1 #include <bits/stdc++.h>
  2 #define pii pair<int,int>
  3 using namespace std;
  4 const int INF = 0x3f3f3f3f;
  5 const int maxn = 404;
  6 struct arc {
  7     int to,w,next;
  8     arc(int x = 0,int y = 0,int z = -1) {
  9         to = x;
 10         w = y;
 11         next = z;
 12     }
 13 } e[maxn*maxn*2];
 14 int hd[maxn],hd2[maxn],d[maxn],cur[maxn],tot,n,m;
 15 void add(int *head,int u,int v,int w) {
 16     e[tot] = arc(v,w,head[u]);
 17     head[u] = tot++;
 18 }
 19 void dijkstra() {
 20     priority_queue<pii,vector<pii>,greater<pii > >q;
 21     memset(d,0x3f,sizeof d);
 22     bool done[maxn] = {false};
 23     q.push(pii(d[1] = 0,1));
 24     while(!q.empty()) {
 25         int u = q.top().second;
 26         q.pop();
 27         if(done[u]) continue;
 28         done[u] = true;
 29         for(int i = hd[u]; ~i; i = e[i].next) {
 30             if(d[e[i].to] > d[u] + e[i].w) {
 31                 d[e[i].to] = d[u] + e[i].w;
 32                 q.push(pii(d[e[i].to],e[i].to));
 33             }
 34         }
 35     }
 36 }
 37 bool bfs() {
 38     queue<int>q;
 39     memset(d,-1,sizeof d);
 40     d[1] = 1;
 41     q.push(1);
 42     while(!q.empty()) {
 43         int u = q.front();
 44         q.pop();
 45         for(int i = hd2[u]; ~i; i = e[i].next) {
 46             if(e[i].w && d[e[i].to] == -1) {
 47                 d[e[i].to] = d[u] + 1;
 48                 q.push(e[i].to);
 49             }
 50         }
 51     }
 52     return d[n] > -1;
 53 }
 54 int dfs(int u,int low) {
 55     if(u == n) return low;
 56     int tmp = 0,a;
 57     for(int &i = cur[u]; ~i; i = e[i].next) {
 58         if(e[i].w && d[e[i].to] == d[u]+1&&(a=dfs(e[i].to,min(low,e[i].w)))) {
 59             e[i].w -= a;
 60             e[i^1].w += a;
 61             tmp += a;
 62             low -= a;
 63             if(!low) break;
 64         }
 65     }
 66     if(!tmp) d[u] = -1;
 67     return tmp;
 68 }
 69 int dinic() {
 70     int ret = 0;
 71     while(bfs()) {
 72         memcpy(cur,hd2,sizeof cur);
 73         ret += dfs(1,INF);
 74     }
 75     return ret;
 76 }
 77 void solve(int u) {
 78     if(u == 1) printf("%d",u);
 79     else printf(" %d",u);
 80     if(u == n) {
 81         putchar('
');
 82         return;
 83     }
 84     for(int i = hd2[u]; ~i; i = e[i].next) {
 85         if((~i&1) && !e[i].w) {
 86             e[i].w = 1;
 87             solve(e[i].to);
 88             break;
 89         }
 90     }
 91 }
 92 int main() {
 93     int u,v,w;
 94     while(~scanf("%d%d",&n,&m)) {
 95         memset(hd,-1,sizeof hd);
 96         memset(hd2,-1,sizeof hd2);
 97         for(int i = tot = 0; i < m; ++i) {
 98             scanf("%d%d%d",&u,&v,&w);
 99             add(hd,u,v,w);
100             add(hd,v,u,w);
101         }
102         dijkstra();
103         for(int i = 1; i <= n; ++i) {
104             for(int j = hd[i]; ~j; j = e[j].next) {
105                 if(d[e[j].to] == d[i] + e[j].w) {
106                     add(hd2,i,e[j].to,1);
107                     add(hd2,e[j].to,i,0);
108                 }
109             }
110         }
111         if(dinic() >= 2) {
112             solve(1);
113             solve(1);
114         } else puts("No solution");
115     }
116     return 0;
117 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4746474.html