1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | /// Raihan Ruhin /// CSE, Jahangirnagar University. /// Dhaka-Bangladesh. /// id: raihanruhin (topcoder / codeforces / codechef / uva ), 3235 (lightoj) /// mail: raihanruhin@ (yahoo / gmail / facebook) /// blog: ruhinraihan.blogspot.com #include<bits/stdc++.h> using namespace std; #define SET(a) memset(a,-1,sizeof(a)) #define CLR(a) memset(a,0,sizeof(a)) #define PI acos(-1.0) #define MOD 1000000007 #define MX 502 struct Z { int v, cost; bool operator < (const Z &p) const { return p.cost<cost; } }; Z z, z2; int shortest[MX], forbidden[MX][MX]; vector<Z> adj[MX]; vector<int> parent[MX]; priority_queue<Z>pq; int dijkstra(int src, int dst, int node) { for(int i=0; i<node; i++) shortest[i]=1e9; z.v = src; z.cost = 0; pq.push(z); shortest[src]=0; while(!pq.empty()) { z = pq.top(); pq.pop(); int l = adj[z.v].size(); for(int i=0; i<l; i++) { z2 = adj[z.v][i]; if(forbidden[z.v][z2.v]==1) continue; if(shortest[z2.v]>shortest[z.v]+z2.cost) { parent[z2.v].clear(); parent[z2.v].push_back(z.v); z2.cost = shortest[z2.v]= shortest[z.v]+z2.cost; pq.push(z2); } else if(shortest[z2.v]==shortest[z.v]+z2.cost) parent[z2.v].push_back(z.v); } } return shortest[dst]; } void removeEdge(int src, int dst) { if(dst==src) return; for(int i=0;i<parent[dst].size();i++) { forbidden[ parent[dst][i] ][dst] = 1; removeEdge(src, parent[dst][i]); } return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int tc,kk=1, node, edge, u, v, cost, src, dst; while(cin>>node>>edge && node) { cin>>src>>dst; for(int i=0; i<edge; i++) { cin>>u>>v>>cost; z.v = v; z.cost = cost; adj[u].push_back(z); } int tmp = 1e9; int mn = dijkstra(src, dst, node); //initial if(mn<1e9) { removeEdge(src, dst); //remove edges of shortest tmp = dijkstra(src, dst, node); //second time } if(tmp==1e9) tmp = -1; cout<<tmp<<endl; CLR(forbidden); for(int i=0;i<node;i++) { adj[i].clear(); parent[i].clear(); } } return 0; } |
Sunday, 18 June 2017
Almost Shortest Path (UVa 12144, UVaLive 4210, Regionals 2008 >> Latin America - South America)
Subscribe to:
Post Comments (Atom)
Nextenpon_da1989 Amber Brown https://wakelet.com/wake/w8Xs8jWjDqbjK0Dg4RzXc
ReplyDeletemyoficorlent
Ypistcaperfda Kyle Mohamed Your Uninstaller
ReplyDeleteDriver Easy Pro
Recover My Files
eslauneter