POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)

时间:2023-12-14 15:56:32
Til the Cows Come Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 37662   Accepted: 12836

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
3 4 20
4 5 20
2 3 30 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.

SPFA:

 #include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
#include <algorithm> using namespace std;
const int INF = ;
const int MAX = + ;
int t,n;
struct point
{
int e,w;
};
vector<point> g[MAX];
int dist[MAX];
void spfa(int v)
{
for(int i = ; i <= n; i++)
{
dist[i] = INF;
}
dist[v] = ;
queue<int> que;
que.push(v);
while(que.size())
{
int x = que.front();
que.pop();
int len = g[x].size();
for(int i = ; i < len; i++)
{
int y = g[x][i].e;
if(dist[y] > dist[x] + g[x][i].w)
{
dist[y] = dist[x] + g[x][i].w;
que.push(y);
}
}
}
}
int main()
{
while(scanf("%d%d", &t, &n) != EOF)
{
for(int i = ; i < MAX; i++)
g[i].clear(); while(t--)
{
int s,e,w;
point temp;
scanf("%d%d%d", &s,&e,&w);
temp.w = w;
temp.e = e;
g[s].push_back(temp);
temp.e = s;
g[e].push_back(temp);
} spfa(n);
printf("%d\n",dist[]);
} return ;
}

SPFA

Dijkstra

注意重边问题

 #include <cstring>
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;
const int INF = ;
const int MAX = + ;
int g[MAX][MAX],dist[MAX],vis[MAX];
int t,n;
void Dijkstra()
{
for(int i = ; i <= n; i++)
dist[i] = INF;
memset(vis,,sizeof(vis));
dist[n] = ;
vis[n] = ;
int pos = n;
for(int i = ; i < n; i++)
{
int minn = INF;
for(int j = ; j <= n; j++)
{
if(vis[j] == && dist[j] < minn)
{
minn = dist[j];
pos = j;
}
}
vis[pos] = ;
for(int j = ; j <= n; j ++)
{
if(vis[j] == && dist[j] > dist[pos] + g[pos][j])
dist[j] = dist[pos] + g[pos][j];
}
}
}
int main()
{
while(scanf("%d%d",&t,&n) != EOF)
{
int s,e,w;
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
{
g[i][j] = INF;
}
}
for(int i = ; i < t; i++)
{
scanf("%d%d%d",&s,&e,&w);
if(g[s][e] > w)
g[s][e] = g[e][s] = w;
}
Dijkstra();
printf("%d\n",dist[]);
} }

Ballem_ford

 #include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int INF = ;
const int MAX = + ;
int n,t;
struct point
{
int s,t,w;
};
vector<point> g;
int dist[MAX];
void Ballem_ford(int v)
{
for(int i = ; i <= n; i++)
dist[i] = INF;
dist[v] = ;
for(int j = ; j < n; j++)
{
int len = g.size();
int flag = ;
for(int i = ; i < len; i++)
{
int s = g[i].s;
int t = g[i].t;
int w = g[i].w;
if(dist[t] > dist[s] + w)
{
dist[t] = dist[s] + w;
flag = ;
}
}
if(flag == ) //加个flag 优化一下
break;
}
}
int main()
{
while(scanf("%d%d", &t, &n) != EOF)
{
g.clear();
int s,e,w;
point temp;
for(int i = ; i < t; i++)
{
scanf("%d%d%d", &s,&e,&w);
temp.w = w;
temp.t = e;
temp.s = s;
g.push_back(temp);
temp.t = s;
temp.s = e;
g.push_back(temp);
}
Ballem_ford(n);
printf("%d\n",dist[]);
}
}