Til the Cows Come Home
Description
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.
Input
* 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.
Output
* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
Sample Input
5 5 1 2 20 2 3 30 3 4 20 4 5 20 1 5 100
Sample Output
90
Hint
INPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
dijkstra代码:
#include<stdio.h>
#include<string.h>
#define N 1000+10
const int INF=0x3f3f3f3f;
int map[N][N];
int n,m;
void dijkstra()
{
int i,j,min,v;
int dis[N];
bool vis[N];
for(i=1;i<=n;i++)//初始化起点到其余各个顶点的路程并且初始化vis数组
{
vis[i]=0;
dis[i]=map[1][i];
}
vis[1]=1;
for(i=1;i<=n-1;i++)
{
min=INF;
for(j=1;j<=n;j++)//找到离起点最近的顶点
{
if(!vis[j]&&dis[j]<min)
{
min=dis[j];
v=j;
}
}
vis[v]=1;
for(j=1;j<=n;j++)//松弛
{
if(!vis[j]&&dis[j]>map[v][j]+dis[v])
dis[j]=map[v][j]+dis[v];
}
}
printf("%d\n",dis[n]);
}
int main()
{
while(~scanf("%d%d",&m,&n))
{
int i,j;
for(i=1; i<=n; i++)//初始化
for(j=1; j<=n; j++)
if(i==j)
map[i][j]=0;
else
map[i][j]=map[j][i]=INF;
int x,y,z;
for(i=1;i<=m;i++)//邻接矩阵存图
{
scanf("%d%d%d",&x,&y,&z);
if(map[x][y]>z)
map[x][y]=map[y][x]=z;
}
dijkstra();
}
return 0;
}
bellman_ford代码:
#include<stdio.h>
#define maxn 2005
#define maxv 1005
const int inf=0x3f3f3f3f;
typedef struct
{
int u,v,w;
} Egde;
Egde egde[maxn];
int n,m;
void bell_man_ford()
{
int i,j;
int d[maxv];
for(i=2; i<=n; i++)//初始化
d[i]=inf;
d[1]=0;
for(i=1; i<=n-1; i++)//迭代n-1次
{
for(j=1; j<=m; j++)//检查每条边
{
if(d[egde[j].u]>d[egde[j].v]+egde[j].w)
d[egde[j].u]=d[egde[j].v]+egde[j].w;
if(d[egde[j].v]>d[egde[j].u]+egde[j].w)
d[egde[j].v]=d[egde[j].u]+egde[j].w;
}
}
printf("%d\n",d[n]);
}
int main()
{
int x,y,z,i;
while(~scanf("%d%d",&m,&n))
{
for(i=1; i<=m; i++)
{
scanf("%d%d%d",&x,&y,&z);
egde[i].u=x;
egde[i].v=y;
egde[i].w=z;
}
bell_man_ford();
}
return 0;
}
spfa代码:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define maxn 4000+10
const int inf=0x3f3f3f3f;
struct node
{
int u,v,w;
} G[maxn];
int n,m,len;
int first[maxn],next[maxn];
int d[maxn];
bool vis[maxn];
void spfa(int star)
{
int i,j;
for(i=1; i<=n; i++)//初始化到起点的距离
{
d[i]=inf;
vis[i]=0;
}
d[star]=0;
vis[star]=1;//初始时只有起点在队列中
queue<int>q;
q.push(star);
while(!q.empty())
{
star=q.front();
q.pop();
vis[star]=0;//清除“在队列中“的标志
for(i=first[star]; i!=-1; i=next[i])
{
if(d[G[i].v]>d[star]+G[i].w)
{
d[G[i].v]=d[star]+G[i].w;
if(!vis[G[i].v])
{
vis[G[i].v]=1;
q.push(G[i].v);
}
}
}
}
printf("%d\n",d[n]);
}
void add_egde(int u,int v,int w)//邻接表存图
{
G[len].u=u,G[len].v=v,G[len].w=w;
next[len]=first[u];
first[u]=len++;
}
int main()
{
int x,y,z,i;
while(~scanf("%d%d",&m,&n))
{
memset(first,-1,sizeof(first));//初始化first数组,表示1~n顶点暂时都没有边
len=1;
for(i=1; i<=m; i++)
{
scanf("%d%d%d",&x,&y,&z);
add_egde(x,y,z);
add_egde(y,x,z);
}
spfa(1);
}
return 0;
}
ps:开始进军最短路!