您的位置:首页 > 其它

POJ 2387 Til the Cows Come Home

2016-09-10 10:44 441 查看

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.

赤裸裸的最短路,给你T(边数)和N(点数),

x,y,z 代表 x 和 y之间有一条路径,注意是双向边

跑最短路就好了数据挺弱的,我跑的dijkstra

后来试了试,spfa也能行



第一行是用的spfa,第二行是dijkatra

时间都差不多

dijstra

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#define M 1010
#define MAXN 5020
using namespace std;
int dis[M];
int t,n;
struct node {
int to;
int next;
int val;
};
node e[MAXN];
int head[M],tot;
bool vis[M];
inline void read(int&x) {
x=0;char c=getchar();
while(c>'9'||c<'0') c=getchar();
while(c>='0'&&c<='9') x=10*x+c-48,c=getchar();
}
inline void add(int x,int y,int z) {
e[++tot].to=y;
e[tot].val=z;
e[tot].next=head[x];
head[x]=tot;
}
inline int hh() {
read(t);read(n);
for(int i=1;i<=t;i++) {
int x,y,z;
read(x);read(y);read(z);
add(x,y,z);add(y,x,z);
}
for(int i=1;i<=n;i++) dis[i]=1e9;
dis[1]=0;
int mn;
for(int i=1;i<=n;i++) {
mn=1e9;
int k=0;
for(int j=1;j<=n;j++)
if(!vis[j]&&dis[j]<mn) {
mn=dis[j];
k=j;
}
vis[k]=true;
if(k==0) break;
for(int j=head[k];j;j=e[j].next) {
int v=e[j].to;
if(!vis[v]&&dis[v]>dis[k]+e[j].val)
dis[v]=dis[k]+e[j].val;
}
}
printf("%d\n",dis
);
return 0;
}
int hhh=hh();
int main() {;}


spfa

#include<queue>
#include<cstdio>
#include"iostream"
#define M 1010
#define MAXN 5050
using namespace std;
struct no {
int to;
int next;
int val;
};
no a[MAXN];
int head[M],tot;
int t,n;
int dis[M],vis[M];
inline void read(int&x) {
x=0;char c=getchar();
while(c>'9'||c<'0') c=getchar();
while(c>='0'&&c<='9') x=10*x+c-48,c=getchar();
}
inline void add(int x,int y,int z) {
a[++tot].to=y;
a[tot].val=z;
a[tot].next=head[x];
head[x]=tot;
}
inline void spfa(int s) {
queue<int> q;
dis[s]=0;
q.push(s);
vis[s]=1;
while(!q.empty()) {
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];i;i=a[i].next) {
int v=a[i].to;
if(dis[v]>dis[u]+a[i].val) {
dis[v]=dis[u]+a[i].val;
if(!vis[v]) {
q.push(v);
vis[v]=true;
}
}
}
}
}
inline int hh() {
read(t);read(n);
for(int i=1;i<=t;i++) {
int x,y,z;
read(x);read(y);read(z);
add(x,y,z);add(y,x,z);
}
for(int i=1;i<=n;i++) dis[i]=1e9;
spfa(1);
printf("%d\n",dis
);
return 0;
}
int hhh=hh();
int main() {;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: