POJ 2837 Til the Cows Come Home
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 45515 | Accepted: 15434 |
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
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int w[][],dis[],team[*];
bool exist[];
void SPFA(int s)
{
int head=,tail=,k=;
team[head]=s;
dis[s]=;
while(head<tail)
{
k=team[head];
exist[k]=false;
for(int i=s;i>=;i--)
{
if((w[k][i]>)&&(dis[i]>dis[k]+w[k][i]))
{
dis[i]=dis[k]+w[k][i];
if(!exist[i])
{
team[tail++]=i;
exist[i]=true; }
}
}
head++;
}
// return dis[n];
}
int main()
{
int T,N,x,y,z;
cin>>T>>N;
for(int i=;i<=T;i++)
{
cin>>x>>y>>z;
if(w[x][y]==) w[x][y]=w[y][x]=z;
else if(w[x][y]>z) w[y][x]=w[x][y]=z; }
for(int i=;i<=;i++) dis[i]=;
SPFA(N);
cout<<dis[];
return ;
}
POJ 2837 Til the Cows Come Home的更多相关文章
- 【POJ - 2387】Til the Cows Come Home(最短路径 Dijkstra算法)
Til the Cows Come Home 大奶牛很热爱加班,他和朋友在凌晨一点吃完海底捞后又一个人回公司加班,为了多加班他希望可以找最短的距离回到公司.深圳市里有N个(2 <= N < ...
- 【POJ】2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 题意:求从1到n的最短路 题解:板子题.spfa. 代码: #include<iostream> #include& ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- 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 (图论,最短路径)
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代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- 怒学三算法 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 ...
- POJ 2387 Til the Cows Come Home 【最短路SPFA】
Til the Cows Come Home Description Bessie is out in the field and wants to get back to the barn to g ...
随机推荐
- 设计模式 ( 十八 ) 策略模式Strategy(对象行为型)
设计模式 ( 十八 ) 策略模式Strategy(对象行为型) 1.概述 在软件开发中也经常遇到类似的情况,实现某一个功能有多种算法或者策略,我们能够依据环境或者条件的不同选择不同的算法或者策略来完毕 ...
- Android操作系统服务(Context.getSystemService() )
getSystemService是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象.下面介绍系统相应的服务: 传入 ...
- Docker学习笔记2
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互之间不会有任何 ...
- Android获取网页上的图片的代码
public Bitmap getWebBitmap(String imgUrl) { Bitmap bitmap =null; try { InputStream inputStream = nul ...
- 源码-hadoop1.1.0-core-org.apache.hadoop.classification
里面放着两个注解类:InterfaceAudience和InterfaceStability. InterfaceAudience 类包含三个注解类型,用来被说明被他们注解的类型的潜在的使用范围(au ...
- Subsequence poj 3061 二分(nlog n)或尺取法(n)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9236 Accepted: 3701 Descr ...
- MySQL(22):事务管理之 事务回滚
1. 在操作事务的时候,如果发现当前事务操作是不合理的,此时只要还没有提交事务,就可以通过回滚取消当前事务,接下来就针对事务的回滚进行详细讲解. 2. 为了演示回滚操作,在上一个笔记案例基础之上,此时 ...
- CSS3: border-radius边框圆角详解
border-radius 基本语法: border-radius : none | <length>{1,4} [/ <length>{1,4} ]? 取值范围: <l ...
- SQL查询中的in与join效率比较
大多数情况下,程序员比较喜欢使用in来查询符合某些条件的数据,最近在查询某个角色有哪些用户的方法中,使用了in语句: ) FROM baseuser AND BaseUser.Id IN (SELEC ...
- 【MongoDB】增删改查基本操作
查看所有数据库 show dbs 切换数据库(若不存在,会自动创建) use databasename 删除当前数据库 db.dropDatabase() MongoDB中没有表,只有集合. 插入集合 ...