题目:
Til the Cows Come Home
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 46097 | Accepted: 15654 |
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.
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.
* 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.
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
基础最短路,注意是双向边。
代码:
#include <stdio.h>
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <math.H>
#include <limits.h>
using namespace std;
const int maxn=1009;
const int inf=(INT_MAX)/3;
typedef pair<int,int> p;
struct edge{
int to;
int cost;
};
int n,t,d[maxn];
vector<edge> g[maxn];
void dijkstra(int s){//堆优化
priority_queue<p,vector<p>,greater<p> >qu;//堆按照p的first(最短距离)排序,小在前
fill(d,d+maxn,inf);
d[s]=0;
qu.push(p(0,s));//从起点出发到顶点s的最短距离为0
while(!qu.empty()){
p temp=qu.top();
qu.pop();
if(temp.first>d[temp.second]) continue;//跳过更新过程中入队的非最小值
for(int i=0;i<g[temp.second].size();++i){//遍历该顶点连出的每条边
edge e=g[temp.second][i];
if(d[e.to]>d[temp.second]+e.cost){
d[e.to]=d[temp.second]+e.cost;
qu.push(p(d[e.to],e.to));
}
}
}
}
int main(){
int a,b,c;
edge e;
scanf("%d%d",&t,&n);
for(int i=1;i<=t;++i){
scanf("%d%d%d",&a,&b,&c);
e.to=b;
e.cost=c;
g[a].push_back(e);
e.to=a;
g[b].push_back(e);
}
dijkstra(n);
printf("%d\n",d[1]);
return 0;
}
附:dijk普通写法
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1009;
const int inf=0x3f3f3f3f;
int mp[maxn][maxn];
int d[maxn];//顶点s出发的最小距离
bool used[maxn];
int n,m;
void dij(int s){
memset(used,0,sizeof(used));
d[s]=0;
while(1){
int t=-1;
for(int i=1;i<=n;++i){
if(!used[i]&&(t==-1||d[i]<d[t])) t=i;
}
if(t==-1) break;
used[t]=true;
for(int i=1;i<=n;++i){
d[i]=min(d[i],d[t]+mp[t][i]);
}
}
}
int main(){//poj 2387
string a;
cin>>a;
cout<<a[3]<<endl;
return 0;
}