POJ 2387 - Til the Cows Come Home

A - Til the Cows Come Home
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Appoint description:

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 <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

#define N 1005
#define T 2005
#define INF 0x3f3f3f3f
int t,n;
int vis[N];
int g[N][N];
int d[N];

int dijkstra(int u){
memset(vis,0,sizeof(vis));
memset(d,0x3f,sizeof(d));
d[u]=0;

for (int i=0;i<n;i++){
int sum=INF,k;
for (int j=1;j<=n;j++)
if (!vis[j] && d[j]<sum)
sum=d[k=j];
vis[k]=1;
//cout<<k<<endl;
for (int j=1;j<=n;j++)
if (!vis[j] && d[j]>d[k]+g[k][j])
d[j]=d[k]+g[k][j];
}

return d[1];
}

int main(){
while (~scanf("%d%d",&t,&n)){
memset(g,0x3f,sizeof(g));
for (int i=0;i<t;i++){
int u,v,d;
scanf("%d%d%d",&u,&v,&d);
if (g[u][v]>d)
g[u][v]=g[v][u]=d;
}

printf("%d\n",dijkstra(n));
}

return 0;
}

//priority_queue dijkstra
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;

#define N 1005
#define T 2005
#define INF 0x3f3f3f3f
int t,n;
int g[N][N];
int d[N];

struct dnode{
int u,d;
dnode(){}
dnode(int uu,int dd):u(uu),d(dd){}
bool operator < (const dnode &a) const{
return d==a.d?u>a.u:d>a.d;
}
};

int dijkstra(int u){
priority_queue<dnode> q;
memset(d,0x3f,sizeof(d));
d[u]=0;
q.push(dnode(u,d[u]));

while (!q.empty()){
dnode cur=q.top(); q.pop();

for (int i=1;i<=n;i++){
if (d[i]>cur.d+g[cur.u][i]){
d[i]=cur.d+g[cur.u][i];
q.push(dnode(i,d[i]));
}
}
}

return d[1];
}

int main(){
while (~scanf("%d%d",&t,&n)){
memset(g,0x3f,sizeof(g));
for (int i=0;i<t;i++){
int u,v,d;
scanf("%d%d%d",&u,&v,&d);
if (g[u][v]>d)
g[u][v]=g[v][u]=d;
}

printf("%d\n",dijkstra(n));
}

return 0;
}

//bellman_ford
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

#define N 1005
#define T 2005
#define INF 0x3f3f3f3f

int g[N][N];

struct Edge{
int u,v,d;
Edge(int uu,int vv,int dd):u(uu),v(vv),d(dd){}
};
vector<Edge> edge;

int t,n;
int u,v,l;
int d[N];

bool ford(int u){
memset(d,0x3f,sizeof(d));
d[u]=0;

for (int i=1;i<n;i++){
bool flag=false;
for (int j=1;j<edge.size();j++){
u=edge[j].u;
v=edge[j].v;
l=edge[j].d;
if (d[v]>d[u]+l){
d[v]=d[u]+l;
flag=true;
}
}
if (!flag)
return true;
}

for (int j=1;j<edge.size();j++)
if (d[edge[j].v]>d[edge[j].u]+edge[j].d)
return false;

return true;
}

int main(){
while (~scanf("%d%d",&t,&n)){
memset(g,0x3f,sizeof(g));
for (int i=0;i<t;i++){
scanf("%d%d%d",&u,&v,&l);
if (g[u][v]>l)
g[u][v]=g[v][u]=l;
}

for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (g[i][j]!=INF)
edge.push_back(Edge(i,j,g[i][j]));

if (ford(n)){
printf("%d\n",d[1]);
}
}

return 0;
}

//SPFA
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

#define N 1005
#define T 2005
#define INF 0x3f3f3f3f

int g[N][N];
int t,n;
int u,v,l;
int d[N],vis[N];
int cnt[N];

int SPFA(int u){
queue<int> q;
memset(d,INF,sizeof(d));
memset(vis,0,sizeof(vis));
q.push(u); vis[u]=1; d[u]=0;

while (!q.empty()){
u=q.front(); q.pop();
vis[u]=0;
for (int v=1;v<=n;v++){
if (d[u]+g[u][v] < d[v]){
d[v]=d[u]+g[u][v];
if (!vis[v]){
vis[v]=1;
q.push(v);
}
}
}
}

return 1;
}

int main(){
while (~scanf("%d%d",&t,&n)){
memset(g,0x3f,sizeof(g));
for (int i=0;i<t;i++){
scanf("%d%d%d",&u,&v,&l);
if (g[u][v]>l)
g[u][v]=g[v][u]=l;
}

if (SPFA(n)){
printf("%d\n",d[1]);
}
}

return 0;
}