POJ 2387 Til the Cows Come Home(图论几个基本算法的初探)
2018-03-28 17:53
531 查看
Til the Cows Come Home
DescriptionBessie 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 Input5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100Sample Output90HintINPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.很裸的最短路径问题了。先学了dijkstra和floyd算法。
这道题可以用dijkstra过,floyd会超时。
先贴上最朴素的两种算法//#include <bits/stdc++.h>
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int maxn = 1100;
typedef long long ll;
bool isprime[maxn];
bool used[maxn];
int d[maxn][maxn];
int dis[maxn];
const int inf = 16843009;
int n,m;
void floyd(){
for(int k = 1; k <= n; k++)
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++){
if(d[i][k] < inf && d[k][j] < inf)
d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
}
}
void dijkstra(){//单源最短路
for(int i = 2; i <= n; i++)
dis[i] = d[1][i];
used[1] = 1;
while(1){
int Mmin = inf+1,u=-1;
for(int i = 2; i <= n; i++){
if(dis[i] < Mmin && used[i] == 0){
Mmin = dis[i];
u = i;
}//找出当前离源点最近的并且没有被访问过的点
}
if(u == -1)break;
used[u] = 1;
for(int i = 1; i <= n; i++){
if(d[u][i]<inf)
dis[i] = min(dis[i],dis[u]+d[u][i]);//看是否加入这点会使得距离变短
}
}
}
int main()
{
std::ios::sync_with_stdio(0);
cin.tie(0);
while(cin>>m>>n)
{
//cout<<sizeof(d)<<endl;
memset(d,1,sizeof(d));
memset(dis,0,sizeof(dis));
//cout<<d[1][1]<<endl;
for(int i = 0; i < m; i++){
int a,b,c;
cin>>a>>b>>c;
d[a][b] = min(d[a][b],c);
d[b][a] = min(d[b][a],c);//注意此题有重边,输入的时候记得处理
}
//floyd();
dijkstra();
cout<<d[1]
<<endl;
/*for(int i = 1; i <= n; i++)
for(int j = i+1; j <= n; j++)
cout<<"the distance between "<<i<<" and "<<j<<" is "<<d[i][j]<<endl;*/
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 65500 | Accepted: 22064 |
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 Input5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100Sample Output90HintINPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.很裸的最短路径问题了。先学了dijkstra和floyd算法。
这道题可以用dijkstra过,floyd会超时。
先贴上最朴素的两种算法//#include <bits/stdc++.h>
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int maxn = 1100;
typedef long long ll;
bool isprime[maxn];
bool used[maxn];
int d[maxn][maxn];
int dis[maxn];
const int inf = 16843009;
int n,m;
void floyd(){
for(int k = 1; k <= n; k++)
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++){
if(d[i][k] < inf && d[k][j] < inf)
d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
}
}
void dijkstra(){//单源最短路
for(int i = 2; i <= n; i++)
dis[i] = d[1][i];
used[1] = 1;
while(1){
int Mmin = inf+1,u=-1;
for(int i = 2; i <= n; i++){
if(dis[i] < Mmin && used[i] == 0){
Mmin = dis[i];
u = i;
}//找出当前离源点最近的并且没有被访问过的点
}
if(u == -1)break;
used[u] = 1;
for(int i = 1; i <= n; i++){
if(d[u][i]<inf)
dis[i] = min(dis[i],dis[u]+d[u][i]);//看是否加入这点会使得距离变短
}
}
}
int main()
{
std::ios::sync_with_stdio(0);
cin.tie(0);
while(cin>>m>>n)
{
//cout<<sizeof(d)<<endl;
memset(d,1,sizeof(d));
memset(dis,0,sizeof(dis));
//cout<<d[1][1]<<endl;
for(int i = 0; i < m; i++){
int a,b,c;
cin>>a>>b>>c;
d[a][b] = min(d[a][b],c);
d[b][a] = min(d[b][a],c);//注意此题有重边,输入的时候记得处理
}
//floyd();
dijkstra();
cout<<d[1]
<<endl;
/*for(int i = 1; i <= n; i++)
for(int j = i+1; j <= n; j++)
cout<<"the distance between "<<i<<" and "<<j<<" is "<<d[i][j]<<endl;*/
}
return 0;
}
相关文章推荐
- POJ 2387 Til the Cows Come Home (图论,最短路径)
- POJ-2387 Til the Cows Come Home(Dijsktra算法求最短路径)
- Dijstra算法:Til the Cows Come Home POJ - 2387
- Poj 2387 Til the Cows Come Home(Dijkstra 最短路径)
- [kuangbin带你飞]专题4 最短路 A - Til the Cows Come Home POJ - 2387
- POJ 2387 Til the Cows Come Home (最短路)
- POJ-2387 Til the Cows Come Home ( 最短路 )
- POJ - 2387 Til the Cows Come Home(最短路)
- poj 2387 Til the Cows Come Home(Dijkstra)
- POJ 2387 Til the Cows Come Home (Dijkstra)
- POJ 2387 Til the Cows Come Home (最短路+Dijkstra)
- POJ - 2387 Til the Cows Come Home——dijkstra
- POJ 2387 Til the Cows Come Home 最短路
- 2387 poj Til the Cows Come Home【dijkstra,经典&&基础】
- poj 2387 Til the Cows Come Home (最短路)
- POJ ~ 2387 ~ Til the Cows Come Home(Dijkstra)
- poj 2387 Til the Cows Come Home(kuangbin带你飞 专题四:最短路)
- POJ 2387 Til the Cows Come Home (Dijkstra)
- poj 2387 Til the Cows Come Home(最短路SPFA+Dijkstra)
- 最短路dij——POJ 2387 Til the Cows Come Home题解