Til the Cows Come Home POJ
来源:互联网 发布:数学老师知乎 编辑:程序博客网 时间:2024/06/04 17:58
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.
* 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.
* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
5 51 2 202 3 303 4 204 5 201 5 100
90
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.
题意:从1到n的最短路;
代码:
#include <iostream>#include<stdio.h>#include<cstdio>#include<iostream>#include<algorithm>#include<math.h>#include<string.h>#include<map>#include<queue>#include<vector>#include<deque>#define ll long long#define inf 0x3f3f3f3f#define mem(a,b) memset(a,b,sizeof(a))using namespace std;int vis[3001];int n,mp[3001][3001];int dis[3001];vector<int>p[3003];void spfa(){ queue<int>que; mem(vis,0); int pos=1; for(int i=1;i<=n;i++) { dis[i]=inf; } while(!que.empty())que.pop(); vis[pos]=1; dis[pos]=0; que.push(1); while(!que.empty()) {//printf("###%d\n",dis[n]); int u=que.front(); que.pop();vis[u]=0; for(int i=0;i<p[u].size();i++) { int v=p[u][i]; int w=mp[u][v]; if(dis[v]>dis[u]+mp[u][v]) { dis[v]=dis[u]+mp[u][v]; if(vis[v]==0) { vis[v]=1; que.push(v); } } } }}int main(){ int m; while(~scanf("%d%d",&m,&n)) { mem(mp,inf); int u,v,w; for(int i=0;i<m;i++) { scanf("%d%d%d",&u,&v,&w); p[u].push_back(v); p[v].push_back(u); if(mp[u][v]>w)//没有这个会wa,因为可能会有重边 mp[u][v]=mp[v][u]=w; } spfa(); printf("%d\n",dis[n]); }}
2》
#include <iostream>#include<stdio.h>#include<cstdio>#include<iostream>#include<algorithm>#include<math.h>#include<string.h>#include<map>#include<queue>#include<vector>#include<deque>#define ll long long#define inf 0x3f3f3f3f#define mem(a,b) memset(a,b,sizeof(a))using namespace std;int vis[3001];int n,mp[3001][3001];int dis[3001];void spfa(){ queue<int>que; mem(vis,0); for(int i=1;i<=n;i++) { dis[i]=inf; } //while(!que.empty())que.pop(); vis[1]=1; dis[1]=0; que.push(1); while(!que.empty()) {//printf("###%d\n",dis[n]); int u=que.front(); que.pop();vis[u]=0; for(int i=1;i<=n;i++)//枚举每一个 { int w=mp[u][i]; if(dis[u]+w<dis[i]&&w!=inf) { dis[i]=dis[u]+w; if(!vis[i]) { vis[i]=1; que.push(i); } } } }}int main(){ int m; while(~scanf("%d%d",&m,&n)) { mem(mp,inf); int u,v,w; for(int i=0;i<m;i++) { scanf("%d%d%d",&u,&v,&w);// p[u].push_back(v);// p[v].push_back(u); if(mp[u][v]>w) mp[u][v]=mp[v][u]=w; } spfa(); printf("%d\n",dis[n]); }}
1 0
- 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
- 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
- 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
- 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
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- idea中的代码简单调试debug
- iOS 随机打乱一个数组的顺序 获得一个新的数组
- qt中路径的解释
- H5中<header><footer><nav>与<div>的区别
- 模板_poj1523SPF_割点以及点连通分支
- Til the Cows Come Home POJ
- 凡是某单元有特定字符的,则删除整行 (VBA )
- 第一二类斯特林数,bell数
- Glassfish-MariaDB
- 织梦首页调用单页栏目内容和文章内容的几种方法
- VMware下minimal CentOS7 安装VMware tools
- Android技术积累汇总(7月19日更新)
- js基本知识比较系列四:动画
- Linux系统MySQL开启远程连接