POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 46727 | Accepted: 15899 |
Description
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
* 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
Sample Input
5 5 1 2 20 2 3 30 3 4 20 4 5 20 1 5 100
Sample Output
90
Hint
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
Dijkstra()
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
typedef __int64 LL;
const int maxn = 2005;
const int INF = 0x3f3f3f3f;
struct Edge{
int u,v,next;
LL w;
bool operator < (const Edge & a)const
{
return w > a.w;
}
}edge[maxn<<1] ;
int tot = 0,head[maxn];
bool vis[maxn];
LL dis[maxn];
void addedge(int u,int v,LL w)
{
edge[tot] = (Edge){u,v,head[u],w
};
head[u] = tot++;
}
void Dijkstra()
{
priority_queue<Edge>que;
Edge p;
memset(dis,INF,sizeof(dis));
memset(vis,false,sizeof(vis));
p.v = 1;
que.push(p);
dis[1] = 0;
while (!que.empty())
{
p = que.top();
que.pop();
int u = p.v;
if (vis[u]) continue;
vis[u] = true;
for (int i = head[u];i != -1;i = edge[i].next)
{
int v = edge[i].v;
if (dis[u] + edge[i].w < dis[v])
{
dis[v] = dis[u] + edge[i].w;
p.u = u,p.v = v,p.w = dis[v];
que.push(p);
}
}
}
}
int main()
{
//freopen("input.txt","r",stdin);
int T,N,u,v;
LL w;
memset(head,-1,sizeof(head));
scanf("%d%d",&T,&N);
for (int i = 0;i < T;i++)
{
scanf("%d%d%I64d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
Dijkstra();
printf("%I64d\n",dis[N]);
return 0;
}
spfa()
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAX_N = 1005;
bool flag[MAX_N];
int edge[MAX_N][MAX_N];
void spfa(int n)
{
int dis[MAX_N];
queue<int>que;
memset(flag,false,sizeof(flag));
memset(dis,0x3f3f3f3f,sizeof(dis));
dis[1] = 0;
que.push(1);
flag[1] = true;
while (!que.empty())
{
int curval = que.front();
que.pop();
flag[curval] = false;
for (int i = 1;i <= n;i++)
{
if (dis[curval] < dis[i] - edge[curval][i])
{
dis[i] = dis[curval] + edge[curval][i];
if (!flag[i])
{
que.push(i);
flag[i] = true;
}
}
}
}
printf("%d\n",dis[n]);
}
int main()
{
int N,T;
while (~scanf("%d%d",&T,&N))
{
int u,v,w;
for (int i = 1;i <= N;i++)
{
for (int j = 1;j <= i;j++)
{
if (i == j) edge[i][j] = 0;
else edge [i][j] = edge[j][i] = INF;
}
}
for (int i = 0;i < T;i++)
{
scanf("%d%d%d",&u,&v,&w);
/*if (w < edge[u][v])
{
edge[u][v] = edge[v][u] = w;
}*/
edge[u][v] = edge[v][u] = min(w,edge[u][v]);
}
spfa(N);
}
return 0;
}
POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)的更多相关文章
- POJ 2387 Til the Cows Come Home(模板——Dijkstra算法)
题目连接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...
- POJ 2387 Til the Cows Come Home(最短路模板)
题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...
- POJ 2387 Til the Cows Come Home --最短路模板题
Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...
- POJ 2387 Til the Cows Come Home (图论,最短路径)
POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...
- POJ.2387 Til the Cows Come Home (SPFA)
POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化)
Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...
- POJ 2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
- 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33015 Accepted ...
- POJ 2387 Til the Cows Come Home (最短路 dijkstra)
Til the Cows Come Home 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Bessi ...
随机推荐
- Using View and Data API with Meteor
By Daniel Du I have been studying Meteor these days, and find that Meteor is really a mind-blowing f ...
- Android Weekly Notes Issue #233
Android Weekly Issue #233 November 27th, 2016 Android Weekly Issue #233 本期内容包括: 用Mockito做RxJava的单元测试 ...
- 【代码笔记】iOS-柱状图
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...
- MyEclipse web项目导入Eclipse,详细说明
最近导入一个MyEclipse的项目,具体是:spring4.1的web项目,同时遇到了一些问题,总结一下. 1.进入项目目录,找到.project文件,打开.增加一个<buildCommand ...
- SOA服务类项目开发模式
开发模式 以需求用例为基,Cas e&Coding两条线并行,服务(M)&消费(VC)分离,单元.接口.功能.集成四层质量管理,自动化集成.测试.交付全程支持. 3个大阶段(需求分析阶 ...
- 批处理bat 命令
1.批处理常用符号: - echo 打开回显或关闭请求回显功能,或显示消息.如果没有任何参数,echo 命令将显示当前回显设置 语法:@echo [{ on|off }] echo{"显示 ...
- CentOS(5.8/6.7)linux生产环境若干优化实战
CentOS系统安装之后并不能立即投入生产环境使用,往往需要先经过我们运维人员的优化才行.在此讲解几点关于Linux系统安装后的基础优化操作.注意:本次优化都是基于CentOS(5.8/6.7). 下 ...
- x01.os.21: print "Loading..."
Linux Inside 是中文版,值得下载一读. 先把目标设低点,开机进入后,在屏幕上打印“Loading..."即可.由于要在 bochs 中运行,首先就是安装 bochs.Oldlin ...
- jni操作jobject
一. 注册JNI函数 1. 静态方法 一般使用javah进行编译,生成很长的文件名和函数名字,这个书写不方便,影响运行效率. 2. 动态注册 使用JNINativeMe ...
- MVC中在RAZOR 模板里突然了现了 CANNOT RESOLVE SYMBOL ‘VIEWBAG’ 的错误提示
然后在Razor中出现了@ViewBag的不可用,@Url不可用,@Html 这些变量都不能用了. 异常提示: 编译器错误消息: CS0426: 类型“XX.Model.System”中不存在类型名称 ...