Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.
Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
* Line 1: Two integers: T and N
* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
5 5 1 2 20 2 3 30 3 4 20 4 5 20 1 5 100
90
INPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
解法一 Bellman-ford 算法
#include<cstdio>
#include<cstring>
#include<algorithm>
#define M 0x3f3f3f3f
using namespace std;
struct edge
{
int from,to,cost;
}es[2010];
int d[1010];
int t,n;
void shortpath(int s)
{
for(int i=1;i<=n;i++)
d[i]=M;
d[s]=0;
while(true)
{
bool up=false;
for(int i=0;i<t;i++)
{
edge e=es[i];
if(d[e.from]!=M&&d[e.to]>d[e.from]+e.cost)
{
d[e.to]=d[e.from]+e.cost;
up=true;
}
if(d[e.to]!=M&&d[e.from]>d[e.to]+e.cost)
{
d[e.from]=d[e.to]+e.cost;
up=true;
}
}
if(!up)
break;
}
}
int main()
{
while(~scanf("%d%d",&t,&n))
{
for(int i=0;i<t;i++)
{
scanf("%d%d%d",&es[i].from,&es[i].to,&es[i].cost);
}
shortpath(1);
printf("%d\n",d[n]);
}
return 0;
}
解法二 迪杰斯塔拉算法
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int M =2010;
const int inf =0x3f3f3f3f;
pair<int,int>p[M];
int cost[M][M];
bool used[M];
int d[M];
int t,n;
void dijkstra()
{
int k,mini;
for(int i=1;i<=n;i++)
d[i]=cost[1][i];
for(int i=1;i<=n;i++)
{
mini=inf;
for(int j=1;j<=n;j++)
{
if(!used[j]&&d[j]<mini)
{
mini=d[j];
k=j;
}
}
used[k]=true;
for(int u=1;u<=n;u++)
d[u]=min(d[u],d[k]+cost[k][u]);
}
}
int main()
{
int x,y,c;
while(~scanf("%d%d",&t,&n))
{
for(int i=0;i<=n;i++)
{
for(int j=0;j<=n;j++)
{
if(i==j)
cost[i][j]=0;
else
cost[i][j]=inf;
}
used[i]=false;
d[i]=inf;
}
for(int i=0;i<t;i++)
{
scanf("%d%d%d",&x,&y,&c);
p[i]=make_pair(x,y);
if(cost[x][y]>c)
cost[x][y]=cost[y][x]=c;
}
dijkstra();
printf("%d\n",d[n]);
}
return 0;
}
解法三 spfa
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int M=4000;
const int inf=0x3f3f3f3f;
struct node
{
int now,to,next;
int v;
}e[M];
int head[M];
int vis[M];
int dis[M];
int cmp;
int t,n;
void init(int a,int b,int c)
{
e[cmp].to=b;
e[cmp].now=a;
e[cmp].next=head[a];
e[cmp].v=c;
head[a]=cmp++;
}
void spfa(int st)
{
memset(vis,0,sizeof(vis));
for(int i=i;i<=n;i++)
dis[i]=inf;
dis[st]=0;
vis[st]=1;
queue<int>q;
q.push(st);
while(q.size())
{
int x=q.front();
q.pop();
vis[x]=0;
for(int i=head[x];i!=-1;i=e[i].next)
{
int y=e[i].to;
if(dis[y]>dis[x]+e[i].v)
{
dis[y]=dis[x]+e[i].v;
if(!vis[y])
{
q.push(y);
vis[y]=1;
}
}
}
}
}
int main()
{
int x,y,z;
while(~scanf("%d%d",&t,&n))
{
memset(head,-1,sizeof(head));
cmp=0;
for(int i=1;i<=t;i++)
{
scanf("%d%d%d",&x,&y,&z);
init(x,y,z);
init(y,x,z);
}
spfa(1);
printf("%d\n",dis[n]);
}
return 0;
}