Til the Cows Come Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 37662   Accepted: 12836

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.

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.

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
3 4 20
4 5 20
2 3 30 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.

SPFA:

 #include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
#include <algorithm> using namespace std;
const int INF = ;
const int MAX = + ;
int t,n;
struct point
{
int e,w;
};
vector<point> g[MAX];
int dist[MAX];
void spfa(int v)
{
for(int i = ; i <= n; i++)
{
dist[i] = INF;
}
dist[v] = ;
queue<int> que;
que.push(v);
while(que.size())
{
int x = que.front();
que.pop();
int len = g[x].size();
for(int i = ; i < len; i++)
{
int y = g[x][i].e;
if(dist[y] > dist[x] + g[x][i].w)
{
dist[y] = dist[x] + g[x][i].w;
que.push(y);
}
}
}
}
int main()
{
while(scanf("%d%d", &t, &n) != EOF)
{
for(int i = ; i < MAX; i++)
g[i].clear(); while(t--)
{
int s,e,w;
point temp;
scanf("%d%d%d", &s,&e,&w);
temp.w = w;
temp.e = e;
g[s].push_back(temp);
temp.e = s;
g[e].push_back(temp);
} spfa(n);
printf("%d\n",dist[]);
} return ;
}

SPFA

Dijkstra

注意重边问题

 #include <cstring>
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;
const int INF = ;
const int MAX = + ;
int g[MAX][MAX],dist[MAX],vis[MAX];
int t,n;
void Dijkstra()
{
for(int i = ; i <= n; i++)
dist[i] = INF;
memset(vis,,sizeof(vis));
dist[n] = ;
vis[n] = ;
int pos = n;
for(int i = ; i < n; i++)
{
int minn = INF;
for(int j = ; j <= n; j++)
{
if(vis[j] == && dist[j] < minn)
{
minn = dist[j];
pos = j;
}
}
vis[pos] = ;
for(int j = ; j <= n; j ++)
{
if(vis[j] == && dist[j] > dist[pos] + g[pos][j])
dist[j] = dist[pos] + g[pos][j];
}
}
}
int main()
{
while(scanf("%d%d",&t,&n) != EOF)
{
int s,e,w;
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
{
g[i][j] = INF;
}
}
for(int i = ; i < t; i++)
{
scanf("%d%d%d",&s,&e,&w);
if(g[s][e] > w)
g[s][e] = g[e][s] = w;
}
Dijkstra();
printf("%d\n",dist[]);
} }

Ballem_ford

 #include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int INF = ;
const int MAX = + ;
int n,t;
struct point
{
int s,t,w;
};
vector<point> g;
int dist[MAX];
void Ballem_ford(int v)
{
for(int i = ; i <= n; i++)
dist[i] = INF;
dist[v] = ;
for(int j = ; j < n; j++)
{
int len = g.size();
int flag = ;
for(int i = ; i < len; i++)
{
int s = g[i].s;
int t = g[i].t;
int w = g[i].w;
if(dist[t] > dist[s] + w)
{
dist[t] = dist[s] + w;
flag = ;
}
}
if(flag == ) //加个flag 优化一下
break;
}
}
int main()
{
while(scanf("%d%d", &t, &n) != EOF)
{
g.clear();
int s,e,w;
point temp;
for(int i = ; i < t; i++)
{
scanf("%d%d%d", &s,&e,&w);
temp.w = w;
temp.t = e;
temp.s = s;
g.push_back(temp);
temp.t = s;
temp.s = e;
g.push_back(temp);
}
Ballem_ford(n);
printf("%d\n",dist[]);
}
}

POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)的更多相关文章

  1. poj2387 Til the Cows Come Home 最短路径dijkstra算法

    Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...

  2. poj2387 Til the Cows Come Home(Dijkstra)

    题目链接 http://poj.org/problem?id=2387 题意 有n个路标,编号1~n,输入路标编号及路标之间相隔的距离,求从路标n到路标1的最短路径(由于是无向图,所以也就是求从路标1 ...

  3. POJ2387 Til the Cows Come Home 【Dijkstra】

    题目链接:http://poj.org/problem?id=2387 题目大意; 题意:给出两个整数T,N,然后输入一些点直接的距离,求N和1之间的最短距离.. 思路:dijkstra求单源最短路, ...

  4. POJ2387 Til the Cows Come Home (最短路 dijkstra)

    AC代码 POJ2387 Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to ...

  5. POJ.2387 Til the Cows Come Home (SPFA)

    POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...

  6. 怒学三算法 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 ...

  7. (Dijkstra) POJ2387 Til the Cows Come Home

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 81024   Accepted ...

  8. POj2387——Til the Cows Come Home——————【最短路】

    A - Til the Cows Come Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & ...

  9. POJ-2387.Til the Cows Come Home.(五种方法:Dijkstra + Dijkstra堆优化 + Bellman-Ford + SPFA + Floyd-Warshall)

    昨天刚学习完最短路的算法,今天开始练题发现我是真的菜呀,居然能忘记邻接表是怎么写的,真的是菜的真实...... 为了弥补自己的菜,我决定这道题我就要用五种办法写出,并在Dijkstra算法堆优化中另外 ...

随机推荐

  1. js的nextSibling,属性兼容IE和FF等浏览器

    Firefox中 空白字符,比如回车,空格等也算作一个Node 就是firstChild,nextsbiling这两个.下面给出函数吧.还是代码比较说明问题代码都是网上来的.不过要注意的是,getNe ...

  2. C#使用IrisSkin2.dll美化WinForm程序界面

    一.添加控件IrisSkin2.dll. 方法:         1.右键“工具箱”.“添加选项卡”,取名“皮肤”.         2.右键“皮肤”,“选择项”弹出对话框        3.点击“浏 ...

  3. The ServiceClass object does not implement the required method in the following form: OMElement sayHello(OMElement e)

    今天遇到一件诡异的事情,打好的同一个aar包,丢到测试环境tomcat,使用soapui测试,正常反馈结果. 丢到本地tomcat,使用soapui测试,始终报以下错误. <soapenv:En ...

  4. PHP基础16:多维数组

    <?php //1.PHP-两维数组 $cars=array ( array("Volvo",22,18), array("BMW",15,13), ar ...

  5. Jquery 将表单序列化为Json对象

    大家知道Jquery中有serialize方法,可以将表单序列化为一个“&”连接的字符串,但却没有提供序列化为Json的方法.不过,我们可以写一个插件实现. 我在网上看到有人用替换的方法,先用 ...

  6. 用python简单处理图片(1):打开\显示\保存图像

    一提到数字图像处理,可能大多数人就会想到matlab,但matlab也有自身的缺点: 1.不开源,价格贵 2.软件容量大.一般3G以上,高版本甚至达5G以上. 3.只能做研究,不易转化成软件. 因此, ...

  7. Ubuntu Navicat正版永久使用方法

    最近技安不再带自己的mac book来公司工作了,公司多出来一个台式机,配置还挺高.于是乎就拿过来用了. 装上了Ubuntu14.04 LTS版,正常的开发工具如vagrant,vitualbox,s ...

  8. LeetCode:Jump Game I II

    Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...

  9. 恢复windows 的快捷方式打开方法,亲测1-7恢复,

    相信有些用户曾试过错误地把LNK文件的打开方式更改其他文件,导致系统所有的快捷方式都失效.在vista与Windows7系统还不普遍使用的时候,相信大家会有点惊慌失措,不要紧,下面只要大家进行如下操作 ...

  10. [C#]Attribute特性(3)——AttributeUsage特性和特性标识符

    相关文章   [C#]Attribute特性 [C#]Attribute特性(2)——方法的特性及特性参数 AttributeUsage特性 除了可以定制自己的特性来注释常用的C#类型外,您可以用At ...