Harbin Tech OJ Touring (Shortest path DIJ algorithm adjacency table + queue)

Source: Internet
Author: User

Touring
Time limit:1000 MS Memory limit:32767 K
Total submit:257 (users) Total accepted:108 (All users) Rating: Special Judge:no
Description

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.

Input

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.

Output
For each test case, first print a line saying "Scenario #p", where p is the number of the ' the ' test case. Then,if Both Mr Li and Mr Liu can manage to arrive their cities,output the minimum cost they would spend,otherwise output "Can not reah!" on one line. Print a blank line after all test case, even after the last one.
Sample Input

4 5

1 3 4

1 2 100

1 3 200

1 4 300

2 3 50

2 4 100

4 6

1 3 4

1 2 100

1 3 200

1 4 300

2 3 50

2 4 100

3 4 50

Sample Output

Scenario #1

250

Scenario #2

200

Hint

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,&AMP;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;}


Harbin Tech OJ Touring (Shortest path DIJ algorithm adjacency table + queue)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.