最短路模板poj2387
来源:互联网 发布:乐视网络大电影 编辑:程序博客网 时间:2024/05/16 15:00
Til the Cows Come Home
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 52264 Accepted: 17653
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 51 2 202 3 303 4 204 5 201 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.
Source
USACO 2004 November
最短路模板spfa最快
#include<cstdio>#include<cstring>#include<vector>#include<iostream>#include<algorithm>#include<set>#include<queue>#include<map>//multimapusing namespace std;const int inf=1<<29;int n,m;struct node{ int u; int l; int next;};node g[4003];int first[4003];int cnt;int dis[1005];int vis[1005];void add(int a,int b,int c){ g[cnt].u=b; g[cnt].l=c; g[cnt].next=first[a]; first[a]=cnt; cnt++;}void spfa(int a){ for(int i=1;i<=n;i++) { dis[i]=inf; vis[i]=0; } dis[a]=0; vis[a]=a; queue<int>q; q.push(a); while(!q.empty()) { a=q.front(); q.pop(); vis[a]=0; for(int i=first[a];i!=-1;i=g[i].next) { if(dis[g[i].u]>dis[a]+g[i].l) { dis[g[i].u]=dis[a]+g[i].l; if(!vis[g[i].u]) { vis[g[i].u]=1; q.push(g[i].u); } } } }}int main(){ while(scanf("%d%d",&m,&n)==2) { cnt=0; memset(first,-1,sizeof(first)); // cout<<first[2]<<endl; int t1,t2,t3; for(int i=1;i<=m;i++) { scanf("%d%d%d",&t1,&t2,&t3); add(t1,t2,t3); add(t2,t1,t3); } spfa(1); printf("%d\n",dis[n]); } return 0;}Dij次之
#include<iostream>#include<stdio.h>#include<algorithm>#include<math.h>#include<string.h>#include<queue>#include<stack>using namespace std;#define MAX_N 1005#define INF 99999999int e[MAX_N][MAX_N];int dis[MAX_N];int book[MAX_N];int n,m,u,v,minn;void Dijkstra(){ for(int i=1; i<=n; i++) dis[i]=e[1][i]; for(int i=1; i<=n; i++) book[i]=0; book[1]=1; for(int i=1; i<=n; i++) { minn=INF; for(int j=1; j<=n; j++) { if(book[j]==0&&dis[j]<minn) { minn=dis[j]; u=j; } } book[u]=1; for(v=1; v<=n; v++) { if(e[u][v]<INF) { if(dis[v]>dis[u]+e[u][v]&&book[v]==0) dis[v]=dis[u]+e[u][v]; } } }}int main(){ int t1,t2,t3; while(scanf("%d%d",&m,&n)!=EOF) { for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { if(i==j) e[i][j]=0; else e[i][j]=INF; } } for(int i=1; i<=m; i++) { scanf("%d%d%d",&t1,&t2,&t3); if(e[t1][t2]>t3) { e[t1][t2]=t3; e[t2][t1]=t3; } } Dijkstra(); printf("%d\n",dis[n]); } return 0;}
0 0
- 最短路模板poj2387
- 最短路问题模板题POJ2387
- 最短路--poj2387
- 【最短路】poj2387 SPFA
- POJ2387---Til the Cows Come Home (最短路模板)
- poj2387-最短路dijkstra算法
- POJ2387(最短路基础)
- poj2387(最简单的最短路)
- POJ2387 Til the Cows Come Home(最短路模板题)
- 【最短路各种方法求解一题(模板题)】POJ2387 Til the Cows Come Home
- 最短路 dijkstra算法 poj2387 bellman_ford spfa
- POJ2387 最短路入门题 [spfa,dijstra+heap]
- poj2387--dijkstra算法求最短路--终于AC了。。。。
- Til the Cows Come Home poj2387的最短路题目
- poj2387- Til the Cows Come Home (最短路)
- 最短路Floyd(hdu1874),dijstra(poj2387)
- poj2387~Til the Cows Come Home(最短路dijkstra)
- 【POJ2387】Til the Cows Come Home (最短路)
- JavaScript变量、作用域和内存问题(二)——垃圾收集器
- 用Python下载一个网页保存为本地的 HTML文件
- java中的增强型for循环
- java生产者消费者
- linux下的文件的查找
- 最短路模板poj2387
- CentOS7安装MySQL并开启远程访问
- Linux多命令协作:管道及重定向
- 点击鼠标右键崩盘解决方法
- NUDT银河之光校赛总结+校赛 F
- Codeforces gym 101149 L 最短路
- opencv实现对图片的批量读取、处理、存储
- java泛性
- Monkey 使用教程--参数选项说明