Til the Cows Come Home
题目链接->http://poj.org/problem?id=2387
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 64593 | Accepted: 21779 |
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.
Problem Idea
【题意】
题目要求概述一下则是:输入一个节点数为n,边数为m的无向图的各条边,即m条边的信息。
要求你输出从 第1号节点到N号节点的距离的最大值,即 DJ.d[n-1]
【类型】
Dijkstra算法求单源最短路径
【分析】
模板题,入门
【时间复杂度&&优化】
O(nlog n)
题目要求概述一下则是:输入一个节点数为n,边数为m的无向图的各条边,即m条边的信息。
要求你输出从 第1号节点到N号节点的距离的最大值,即 DJ.d[n-1]
【类型】
Dijkstra算法求单源最短路径
【分析】
模板题,入门
【时间复杂度&&优化】
O(nlog n)
Source Code
#include <iostream>
#include <queue>
#include <vector>
#include <cstring>
#include <cstdio>
const int nmax=100+5;
#define INF 1e8
int n,m;
using namespace std;
struct HeapNode{
int d,u;//d为s到各个节点的距离,u为起点
HeapNode(){}
HeapNode(int d,int u):d(d),u(u){}
bool operator <(const HeapNode &rhs)const{//自定义greater算子,优先输出d小的节点
return d>rhs.d;
}
};
struct Edge{
int from,to,dist;
Edge(){}
Edge(int f,int t,int d):from(f),to(t),dist(d){}
};
struct Dijkstra{
int n,m; //n为点数,m为边数
vector<Edge> edges;//边列表,存储各边的编号
vector<int>G[nmax];//邻接表,存储每个节点出发的边编号(从0开始编号),G[u][i]为起点u到节点i的边的编号
bool done[nmax];//是否已永久标号
int d[nmax];//源点s到各个节点的距离,id[i]为源点s到节点i的距离
int p[nmax];//最短路中的上一条弧,p[i]为源点s到节点i的最短路中的最后一条边的编号
Dijkstra(){} //记得写个空的构造函数,要不然DJ会报错
void init(int n){
this->n=n;
for(int i=0;i<n;i++){
G[i].clear();//清空邻接表
}
edges.clear();//清空边列表
}
void AddEdge(int from,int to,int dist){//如果是无向图,每条无向边调用两次AddEdge
edges.push_back(Edge(from,to,dist));//边列表增加一条边
m=edges.size();//边列表中有几条边
G[from].push_back(m-1);//邻接表中的节点数目(下标从0开始)
}
void dijkstra(){//求源点0到其他节点的最短路径
for(int i=0;i<n;i++) d[i]=INF;
//d[s]=0;//记得源点从0开始,不是1开始
d[0]=0; //本题中,源点为节点0
memset(done,0, sizeof(done));
priority_queue<HeapNode>q;
q.push(HeapNode(d[0],0));//本题中优先队列初始化的节点为HeapNode(d[0],0)
while(!q.empty()){
//在所有未标号的节点中,选出d值最小的节点
HeapNode x=q.top();//找到d值最大的节点
q.pop();//弹出队头
int u=x.u;//得到d值最大的节点的起点
//给节点x标记
if(done[u]) continue; //如果当前节点已经标号,则continue
done[u]=1; //若未标号,则标记
//遍历从x出发的所有边(x,y),更新d[y]=min{d[y],d[x]+w[x,y]}
for(int i=0;i<G[u].size();i++){
Edge& e=edges[G[u][i]];//得到从u起点出发到i节点的一条边e
if(d[e.to]>d[u]+e.dist){
d[e.to]=d[u]+e.dist;
p[e.to]=G[u][i];
q.push(HeapNode(d[e.to],e.to));//添加节点y
}
}
}
}
}DJ;
int main() {
while(cin>>n>>m){//输入节点数
DJ.init(n);//切记要初始化,清空边列表和邻接表
while(m--) {
int u,v,d;
scanf("%d%d%d",&u,&v,&d);//输入节点是从1开始的
u--;v--;//记得把输入节点调整成0开始的
DJ.AddEdge(u,v,d);
DJ.AddEdge(v,u,d);
}
DJ.dijkstra();//得到节点0到其他节点的最短路径长度
cout<<DJ.d[n-1]<<endl;//求节点0到节点n-1的最短距离
}
return 0;
}