码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-06-16 09:21:57      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:acm   c++   算法   it   

Touring
Time Limit: 1000 MS Memory 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;
}
 


 
 
 


 

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

标签:acm   c++   算法   it   

原文地址:http://blog.csdn.net/lsgqjh/article/details/46510053

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!