Til the Cows Come Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 36569 Accepted: 12452
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.
Source
USACO 2004 November
首先对Dijkstra和bellman-ford算法进行总结:
最短路径问题这两个算法已经足够。
Dijkstra:适用于权值为非负的图的单源最短路径,用斐波那契堆的复杂度O(E+VlgV)
BellmanFord:适用于权值有负值的图的单源最短路径,并且能够检测负圈,复杂度O(VE)
当权值为非负时,用Dijkstra。
当权值有负值,而且可能存在负圈,则用BellmanFord,能够检测并输出负圈。
裸Dij超时:O(N^2)
一次遍历找到找到最短距离的顶点,再遍历更新其邻顶点
#include <iostream>
#include<cstdio>
#define MAXN 1005
#define INF 0xffffff
using namespace std;
int T,N,G[MAXN][MAXN];
int dis[MAXN];//到点1的距离
int vis[MAXN];
int dij(){
int minVertex=0,minDis;
for(int i=1;i<=N;i++){//找到上一次更新后的最短路径
minDis=INF;
for(int j=1;j<=N;j++){
if(!vis[j]&&dis[j]<minDis){
minDis=dis[j];
minVertex=j;
}
}
vis[minVertex]=1;
for(int j=1;j<=N;j++){
if(!vis[j]&&dis[j]>dis[minVertex]+G[minVertex][j])
dis[j]=dis[minVertex]+G[minVertex][j];
}
if(vis[N]&&dis[N]!=INF) return dis[N];
}
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
while(cin>>T>>N){
for(int i=1;i<=N;i++)
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
if(i==j) G[i][j]=0;
else G[i][j]=G[j][i]=INF;
}
}
for(int i=1;i<=T;i++){
int a,b,cost;
scanf("%d%d%d",&a,&b,&cost);
if(G[a][b]>cost)G[a][b]=G[b][a]=cost;
}
fill(vis,vis+N,0);
for(int i=1;i<=N;i++) dis[i]=G[1][i];
cout<< dij()<<endl;
}
return 0;
}
Dij队列优化(47ms)O(E+V*logV)
遍历更新邻点。不用遍历寻找最短距离的顶点。
#include <iostream>
#include <cstdio>
#include <queue>
#include <string.h>
#define MAX 2010
using namespace std;
const int INF = (1<<31)-1;
typedef pair<int,int> pii;
priority_queue< pii , vector<pii> , greater<pii> > q;//存放松弛过的边及其顶点
int n , m;
int edges[MAX][MAX];
int d[MAX];
bool done[MAX];
void init()
{
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
if(i == j) edges[i][j] = 0;
else edges[i][j] = INF;
}
}
}
void dijkstra(int start)
{
for(int i = 0; i < n; ++i) d[i] = INF
d[start]=0;
memset(done,0,sizeof(done));
q.push(make_pair(d[start],start));
while(!q.empty()){
pii u = q.top();q.pop();
int x = u.second;
if(done[x]) continue;
done[x] = true;
for(int j = 0; j < n; ++j){//遍历并松弛邻边
if( !done[j] && edges[x][j] < INF && d[j] > d[x] + edges[x][j])
{//!done[j]说明j点还没有松弛。edges[x][j] < INF:说明x、j点之间有连通。d[j] > d[x] + edges[x][j]:说明可以松弛
d[j] = d[x] + edges[x][j];
q.push(make_pair(d[j],j));
}
}
}
cout << d[n-1] << endl;
}
int main()
{
while(scanf("%d %d",&m,&n) != EOF){
int u , v , w;
init();
for(int i = 0; i < m; ++i){
scanf("%d %d %d",&u,&v,&w);
if(w < edges[u-1][v-1])
edges[u-1][v-1] = edges[v-1][u-1] = w;
}
dijkstra(0);
}
return 0;
}
Bellman-ford通过(79ms):O(E*V)
#include <iostream>
#include<cstdio>
using namespace std;
#define inf 1<<29
#define MAXM 2005
#define MAXV 1005
typedef struct{
int a,b,w;
}Edge;
Edge edge[MAXM];
int n,m;
void bellman_ford(){
int i,j;
int d[MAXV];
for(i=2;i<=n;i++) d[i]=inf;
d[1]=0;
for(i=1;i<=n;i++){//每个点
for(j=1;j<=m;j++){//每条边
if(d[edge[j].a]>edge[j].w+d[edge[j].b]) d[edge[j].a]=edge[j].w+d[edge[j].b];//重边
if(d[edge[j].b]>edge[j].w+d[edge[j].a]) d[edge[j].b]=edge[j].w+d[edge[j].a];//重边
}
}
printf("%d\n",d[n]);
}
int main(){
int i,a,b,c;
while(~scanf("%d%d",&m,&n)){
for(i=1;i<=m;i++){
scanf("%d%d%d",&a,&b,&c);
edge[i].a=a;
edge[i].b=b;
edge[i].w=c;
}
bellman_ford();
}
return 0;
}