SAMER08A - Almost Shortest Path
Finding the shortest path that goes from a starting point to a destination point given a set of points and route lengths connecting them is an already well known problem, and it's even part of our daily lives, as shortest path programs are widely available nowadays.
Most people usually like very much these applications as they make their lives easier. Well, maybe not that much easier.
Now that almost everyone can have access to GPS navigation devices able to calculate shortest paths, most routes that form the shortest path are getting slower because of heavy traffic. As most people try to follow the same path, it's not worth it anymore to follow these directions.
With this in his mind, your boss asks you to develop a new application that only he will have access to, thus saving him time whenever he has a meeting or any urgent event. He asks you that the program must answer not the shortest path, but the almost shortest path. He defines the almost shortest path as the shortest path that goes from a starting point to a destination point such that no route between two consecutive points belongs to any shortest path from the starting point to the destination.
For example, suppose the figure below represents the map given, with circles representing location points, and lines representing direct, one-way routes with lengths indicated. The starting point is marked as S and the destination point is marked as D. The bold lines belong to a shortest path (in this case there are two shortest paths, each with total length 4). Thus, the almost shortest path would be the one indicated by dashed lines (total length 5), as no route between two consecutive points belongs to any shortest path. Notice that there could exist more than one possible answer, for instance if the route with length 3 had length 1. There could exist no possible answer as well.
Input
The input contains several test cases. The first line of a test case contains two integers N (2 ≤ N ≤ 500) and M (1 ≤ M ≤ 104), separated by a single space, indicating respectively the number of points in the map and the number of existing one-way routes connecting two points directly. Each point is identified by an integer between 0 and N -1. The second line contains two integers S and D, separated by a single space, indicating respectively the starting and the destination points (S ≠ D; 0 ≤ S, D < N).
Each one of the following M lines contains three integers U, V and P (U ≠ V; 0 ≤ U, V < N; 1 ≤ P ≤ 103), separated by single spaces, indicating the existence of a one-way route from U to V with distance P. There is at most one route from a given point U to a given point V, but notice that the existence of a route from U to V does not imply there is a route from V to U, and, if such road exists, it can have a different length. The end of input is indicated by a line containing only two zeros separated by a single space.
Output
For each test case in the input, your program must print a single line, containing -1 if it is not possible to match the requirements, or an integer representing the length of the almost shortest path found.
Example
Input:
7 9
0 6
0 1 1
0 2 1
0 3 2
0 4 3
1 5 2
2 6 4
3 6 2
4 6 4
5 6 1
4 6
0 2
0 1 1
1 2 1
1 3 1
3 2 1
2 0 3
3 0 2
6 8
0 1
0 1 1
0 2 2
0 3 3
2 5 3
3 4 2
4 1 1
5 1 1
3 0 1
0 0
Output:
5
-1
6
题意是给出一个单向图,然后只要这条路径(S->D)的长度和最短路的长度一致,那么这条路上所有的边都删掉之后,再跑一次从S->D的最短路。
反向建边后跑两次dij,然后枚举所有边将属于最短路的边删掉。
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define inf 0x3f3f3f3f 4 #define pii pair<int,int> 5 #define mp make_pair 6 struct Edge{ 7 int u,v,w,next; 8 bool o; 9 }e1[10010],e2[10010]; 10 int tot1,tot2,first1[550],first2[550]; 11 void add1(int u,int v,int w){ 12 e1[tot1].u=u; 13 e1[tot1].v=v; 14 e1[tot1].o=1; 15 e1[tot1].w=w; 16 e1[tot1].next=first1[u]; 17 first1[u]=tot1++; 18 } 19 void add2(int u,int v,int w){ 20 e2[tot2].u=u; 21 e2[tot2].v=v; 22 e2[tot2].o=1; 23 e2[tot2].w=w; 24 e2[tot2].next=first2[u]; 25 first2[u]=tot2++; 26 } 27 int N,M,S,D,i,j,k; 28 bool vis[550]; 29 int d1[550],d2[550]; 30 int dij(int S,int D,int d[],Edge e[],int first[]){ 31 memset(d,inf,sizeof(int)*500); 32 memset(vis,0,sizeof(bool)*500); 33 priority_queue<pii,vector<pii>,greater<pii> > q; 34 q.push(mp(0,S)); 35 d[S]=0; 36 while(!q.empty()){ 37 int u=q.top().second; 38 q.pop(); 39 if(vis[u]) continue; 40 vis[u]=1; 41 for(int i=first[u];i+1;i=e[i].next){ 42 if(e[i].o&&d[e[i].v]>d[u]+e[i].w){ 43 d[e[i].v]=d[u]+e[i].w; 44 q.push(mp(d[e[i].v],e[i].v)); 45 } 46 } 47 } 48 return d[D]==inf?-1:d[D]; 49 } 50 int main() 51 { 52 while(cin>>N>>M&&(N||M)){int u,v,w; 53 cin>>S>>D; 54 memset(first1,-1,sizeof(first1)); 55 memset(first2,-1,sizeof(first2)); 56 tot1=tot2=0; 57 while(M--){ 58 scanf("%d%d%d",&u,&v,&w); 59 add1(u,v,w); 60 add2(v,u,w); 61 } 62 int minn=dij(S,D,d1,e1,first1); 63 dij(D,S,d2,e2,first2); 64 for(i=0;i<tot1;++i){ 65 if(e1[i].w+d1[e1[i].u]+d2[e1[i].v]==minn) e1[i].o=0; 66 } 67 cout<<dij(S,D,d1,e1,first1)<<endl; 68 } 69 return 0; 70 }