The best friends Mr Li and Mr Liu is touring in beautiful country m. m have n cities and m two-way roads in total. Each road connects the cities with fixed length. We assume the cost of car traveling on the road are only related to the length of the road,the longer road the more money To pay. Now,both Mr Li and Mr Liu is in the city C,they has chosen to travel separately by the next time. Mr Li chooses City A with beautiful scenery to is next place, Mr. Liu goes to City B with ancient temples. You is their friend with clever minds,just tell them how to arrive the target places make total costs of them minimum. |
The input file contains sevearl test cases. The first line of all case is positive integers n,and m (3<=n<=5000, 1<=m<=10000). The cities is named from 1 to n.three positive integers C, A, B is follwing. Then,m lines be Given,each line contains three integers i,j and k,indicating One road between I and J is Exists,and Shoul D Pay-K by the car. Process to the end of file. |
The car can carry with both of them at the same time. For case 1:li and Liu can take car together from 1 to 2, and then Li can take car from 2 to 3,liu can take car from 2 to 4 , so the total cost is 100+50+100. #include <cstdio> #include <string.h> #include <queue>using namespace std;const int Inf=5005;const int I=0x1f1f1f1f;int lowa[inf];int lowb[inf];int lowc[inf];int vis[inf];int head[inf];struct Edge{int Next_edge,point,cos t;} edge[inf*inf];struct arc{int num,cost; Arc (int n,int c): Num (n), cost (c) {} ARC () {}; friend BOOL operator< (const ARC &a,const arc &b) {return a.cost>b.cost; }};void dij (int cmd,int *low,int n) {int tt=0; priority_queue<arc>q; low[cmd]=0; Q.push (ARC (cmd,0)); memset (vis,0,sizeof (VIS)); while (Tt<n&&!q.empty ()) {ARC x=q.top (); Q.pop (); if (Vis[x.num]) continue; Vis[x.num]=1; Low[x.num]=x.cost; tt++; for (int e=head[x.num];e!=-1;e=edge[e].next_edge) {if (!vis[edge[e].point]) {Q.pus H (ARC (edge[e].point,edge[e].cost+x.cost)); }}}}int Main () {int n,m,u,v,x,c,a,b,cse=1; while (scanf ("%d%d", &n,&M)!=eof) {memset (head,-1,sizeof (head)); memset (lowa,0x1f,sizeof (Lowa)); memset (lowc,0x1f,sizeof (LOWC)); memset (lowb,0x1f,sizeof (LOWB)); scanf ("%d%d%d", &c,&a,&b); for (int i=1;i<=m;i++) {scanf ("%d%d%d", &u,&v,&x); Edge[i].next_edge=head[u]; Edge[i].point=v; Edge[i].cost=x; Head[u]=i; EDGE[I+M].NEXT_EDGE=HEAD[V]; Edge[i+m].point=u; Edge[i+m].cost=x; Head[v]=i+m; } dij (C,lowc,n); Dij (B,lowb,n); Dij (A,lowa,n); printf ("Scenario #%d\n", cse++); if (lowc[b]>=i| | lowc[a]>=i) {printf ("Can not reah!\n"); Continue } int res=i; for (int i=1;i<=n;i++) {if (lowa[i]+lowb[i]+lowc[i]<res) {Res=lowa[i]+lowb[i]+lo Wc[i]; }} printf ("%d\n\n", res); } return 0;}
|