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
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.
Output
* 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.
Sample Input
5 5 1 2 20 2 3 30 3 4 20 4 5 20 1 5 100Sample Output
90Hint
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.
解:很明显的单源最短路径,但有几点需要注意的地方,题目是先输入的边T,在输入的点N,两点之间可能存在多条禄基金能够,需要选出最短的一条存储。
迪杰斯特拉:
//邻接矩阵
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define inf 0x3fffffff
const int N=1005;
int t,n,a,b,c,e[N][N],dis[N];
bool vis[N];
int main()
{
memset(vis,false,sizeof(vis));
scanf("%d%d",&t,&n);
for(int i=0;i<t;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(!e[a-1][b-1]||c<e[a-1][b-1])//选出最短的一条边存储
e[a-1][b-1]=e[b-1][a-1]=c;
}
for(int i=0;i<n;i++)
dis[i]=e[n-1][i];
vis[n-1]=true;
for(int i=0;i<n-1;i++)
{
int t=inf,u;
for(int j=0;j<n;j++)
{
if(t>dis[j]&&dis[j]&&!vis[j])
{
t=dis[j];
u=j;
}
}
vis[u]=true;
for(int j=0;j<n;j++)
{
if(e[u][j])//判断是否存在从u到j的路径
if(dis[u]+e[u][j]<dis[j]||!dis[j])//判断是否可以松弛,!dis[j]是判断当前是否存在路径,如果不存在则可以松弛
dis[j]=dis[u]+e[u][j];
}
}
printf("%d\n",dis[0]);
}
//邻接表
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<set>
using namespace std;
#define inf 0x3fffffff
const int N=1010;
struct node
{
int v,dis;
node(int a,int b) : v(a),dis(b) {}
};
vector<node>adj[N];
int m,n,dis[N],e[N][N];
bool vis[N];
void dijkstra()
{
fill(dis,dis+N,inf);
memset(vis,false,sizeof(vis));
dis[n-1]=0;
for(int i=0;i<n;i++)
{
int tmp=inf,u=-1;
for(int j=0;j<n;j++)
{
if(!vis[j]&&tmp>dis[j])
{
tmp=dis[j];
u=j;
}
}
if(u==-1) return ;
vis[u]=true;
for(int j=0;j<adj[u].size();j++)
{
int v=adj[u][j].v;
int diss=adj[u][j].dis;
if(dis[v]>dis[u]+diss)
dis[v]=dis[u]+diss;
}
}
}
int main()
{
int u,v,wt;
while(~scanf("%d%d",&m,&n))
{
fill(e[0],e[0]+N*N,inf);
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&wt);
adj[u-1].push_back(node(v-1,wt));
adj[v-1].push_back(node(u-1,wt));
}
dijkstra();
printf("%d\n",dis[0]);
}
}
佛洛依德(会超时,刚学最短路,纯当练手)
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define inf 0x3fffffff
#define LL long long
const int N=1005;
int e[N][N],dis[N];
bool vis[N];
int main()
{
int t,n,a,b,c;
scanf("%d%d",&t,&n);
while(~scanf("%d%d",&t,&n))
{
for(int i=0;i<t;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(!e[a-1][b-1]||c<e[a-1][b-1])
e[a-1][b-1]=e[b-1][a-1]=c;
}
for(int k=0;k<n;k++)
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if((!e[i][j]||e[i][k]+e[k][j]<e[i][j])&&e[i][k]&&e[k][j])
e[i][j]=e[j][i]=e[i][k]+e[k][j];
printf("%d\n",e[0][n-1]);
}
}