您的位置:首页 > 其它

POJ 2387 最短路 贝尔曼福特和迪杰斯特拉双解

2017-04-25 22:28 344 查看
Til the Cows Come Home

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 52016 Accepted: 17570
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.

裸题最短路,仅作练手

代码:
//By Sean Chen
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
using namespace std;

/*Bellman-Ford
struct edge{
int x,y,dis;
};
edge data[2005];
int Map[1005][1005];
int dist[1005];
int t,n;
int a,b,dis;

int main()
{
scanf("%d%d",&t,&n);
memset(dist,inf,sizeof(dist));
memset(Map,inf,sizeof(Map));
dist[1]=0;
for (int i=1;i<=t;i++)
{
scanf("%d%d%d",&a,&b,&dis);
if (dis<Map[a][b])
Map[a][b]=dis;
}
int cnt=0;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
{
if (Map[i][j]!=inf)
{
data[cnt].dis=Map[i][j];
data[cnt].x=i;
data[cnt].y=j;
cnt++;
}
}
for (int i=1;i<n;i++)
for (int j=0;j<cnt;j++)
{
edge temp=data[j];
dist[temp.x]=min(dist[temp.y]+temp.dis,dist[temp.x]);
dist[temp.y]=min(dist[temp.x]+temp.dis,dist[temp.y]);
}
printf("%d",dist
);
return 0;
}
*/
/*dijkstra
int dist[1005];
int Map[1005][1005];

struct node{
int e,dis;
};

vector <node> data[1005];

int n,t;
int visit[1005];
int main()
{
scanf("%d%d",&t,&n);
int a,b,dis;
memset(Map,inf,sizeof(Map));
memset(dist,inf,sizeof(dist));
dist[1]=0;
visit[1]=1;
node temp;
for (int i=0;i<t;i++)
{
scanf("%d%d%d",&a,&b,&dis);
if (dis<Map[a][b])
{
Map[b][a]=dis;
Map[a][b]=dis;
}
}
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
{
if (Map[i][j]!=inf);
{
temp.e=j;
temp.dis=Map[i][j];
data[i].push_back(temp);
}
}
for (int i=0;i<data[1].size();i++)
dist[data[1][i].e]=data[1][i].dis;
for (int i=1;i<n;i++)
{
int Min=inf,minpos;
for (int j=2;j<=n;j++)
if (!visit[j] && dist[j]<Min)
{
Min=dist[j];
minpos=j;
}
visit[minpos]=1;
for (int j=0;j<data[minpos].size();j++)
dist[data[minpos][j].e]=min(dist[data[minpos][j].e],Min+data[minpos][j].dis);
if (minpos==n)
break;
}
printf("%d",dist
);
return 0;
}
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: