NEFU 644 touring compute(SPFA+最短路)

本文介绍了一个旅行计算问题,其中两位朋友需要从同一城市出发前往不同的目的地,并寻求最小化的总旅行成本。通过使用最短路径算法,文章探讨了如何计算两个旅行者到达各自目的地的最低费用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

touring compute

Time Limit 1000ms

Memory Limit 65536K

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


								
							

source


思路:对A,B,C三点分别来次最短路,答案就是MIn(dis[A][i]+dsi[B][i]+dis[C][i];)枚举所有的i


#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int mm=5e3+9;
const int nn=4e4+9;
const int oo=1e9;
int head[nn],next[nn],ver[nn],edge,q[mm],vis[mm];
long long dis[3][mm],cost[nn];
int n,m,C,A,B;
void data()
{
  memset(head,-1,sizeof(head));
  edge=0;
}
void add(int u,int v,int c)
{
  ver[edge]=v;cost[edge]=c;next[edge]=head[u];head[u]=edge++;
  ver[edge]=u;cost[edge]=c;next[edge]=head[v];head[v]=edge++;
}
void spfa(int s,long long*dis)
{ memset(vis,0,sizeof(vis));
  for(int i=0;i<=n;++i)
    dis[i]=oo;
  dis[s]=0;
  int l=0,r=1,z,v;
  q[0]=s;vis[s]=1;
  while(l^r)
  {
    z=q[l++];l%=mm;vis[z]=0;
    for(int i=head[z];i>=0;i=next[i])
    {
      v=ver[i];
      if(dis[v]>dis[z]+cost[i])
       {
         dis[v]=dis[z]+cost[i];
         if(!vis[v])
          vis[v]=1,q[r++]=v,r%=mm;
       }
    }
  }
}
void Min(long long&x,const long long y)
{
  if(x>y)x=y;
}
int main()
{ int a,b,c;int cas=0;
  while(scanf("%d%d",&n,&m)!=EOF)
  { data();
    scanf("%d%d%d",&C,&A,&B);
    for(int i=0;i<m;++i)
    {
      scanf("%d%d%d",&a,&b,&c);
      add(a,b,c);
    }
    spfa(C,dis[0]);
    spfa(A,dis[1]);
    spfa(B,dis[2]);
    long long ans=oo;
    for(int i=1;i<=n;++i)
    {
      Min(ans,dis[0][i]+dis[1][i]+dis[2][i]);
    }
    printf("Scenario #%d\n",++cas);
    if(ans!=oo)
    cout<<ans<<"\n\n";
    else printf("Can not go reach!\n\n");
  }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值