哈理工oj Touring (最短路 dij算法 邻接表 + 队列 )

Touring
Time Limit: 1000 MSMemory Limit: 32767 K
Total Submit: 257(46 users)Total Accepted: 108(39 users)Rating: Special Judge: No
Description

The best friends Mr. Li and Mr. Liu are touring in beautiful country M.

M has n cities and m two-way roads in total. Each road connects two cities with fixed length.We assume that the cost of car traveling on the road is only related to the length of road,the longer road the more money to pay. 

Now,both Mr. Li and Mr. Liu are in the city C,they have chosen to travel separately by the next time.

Mr. Li chooses city A with beautiful scenery to be next place, Mr. Liu goes to city B with ancient temples.

You are 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 each case are two positive integers n,and m(3<=n<=5000, 1<=m<=10000). The cities are named from 1 to n.Three positive integers C, A, B are follwing.Then,m lines are given,each line contains three integers i,j and k,indicating one road between i and j is exists,and should pay cost 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 test case.Then,if both Mr. Li and Mr. Liu can manage to arrive their cities,output the minimum cost they will spend,otherwise output "Can not reah!", in one line.Print a blank line after each 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,cost;
}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.push(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]+lowc[i];
          }
      }
      printf("%d\n\n",res);

  }
  return 0;
}
 


 
 
 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值