哈理工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
    评论
循环比赛日程表是一种常见的算法问题,可以使用递归或迭代的方式来生成日程表。下面是一个使用C++编写的循环比赛日程表的示例代码: ```cpp #include <iostream> #include <vector> using namespace std; void generateSchedule(int teams) { if (teams % 2 != 0) { teams++; // 如果队伍数为奇数,添加一个虚拟队伍 } int rounds = teams - 1; // 总轮次数 int matches = teams / 2; // 每轮的比赛场次 vector<vector<int>> schedule(rounds, vector<int>(matches)); // 初始化第一轮的比赛安排 for (int i = 0; i < matches; i++) { schedule[0][i] = i + 1; } // 生成后续轮次的比赛安排 for (int round = 1; round < rounds; round++) { for (int match = 0; match < matches; match++) { int team1 = schedule[round - 1][match]; int team2; // 计算每个队伍的对手 if (match == 0) { team2 = teams - 1; } else { team2 = schedule[round - 1][match - 1]; } // 考虑虚拟队伍的情况 if (team1 == teams - 1 || team2 == teams - 1) { team1 = (team1 + 1) % (teams - 1); team2 = (team2 + 1) % (teams - 1); } schedule[round][match] = team2; } } // 打印比赛日程表 for (int round = 0; round < rounds; round++) { cout << "Round " << round + 1 << ": "; for (int match = 0; match < matches; match++) { cout << schedule[round][match] << " vs " << teams - schedule[round][match] - 1 << " "; } cout << endl; } } int main() { int teams; cout << "Enter the number of teams: "; cin >> teams; generateSchedule(teams); return 0; } ``` 这段代码中,我们首先根据输入的队伍数计算总轮次数和每轮的比赛场次。然后,使用一个二维向量 `schedule` 来存储比赛安排。我们从第一轮开始,逐轮生成比赛对阵,并将结果存储在 `schedule` 中。最后,打印出比赛日程表。 希望这个示例代码对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值