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. [服务]ftp主动模式和被动模式

    经常忘记这个东西.于是总结下这东西感受下这个协议. FTP连接方式 控制连接:标准端口为21,用于发送FTP命令信息 数据连接:标准端口为20,用于上传.下载数据 数据连接的建立类型: 主动模式:服务 ...

  2. Linux 进程与线程四(加锁--解锁)

    线程共享进程的内存空间,打开的文件描述符,全局变量. 当有多个线程同事访问一块内存空间或者一个变量.一个文件描述符,如果不加控制,那么可能会出现意想不到的结果. 原子操作 对于我们的高级语言(C语言, ...

  3. no.5.print sum

    #-*-coding=utf-8-*- for a in range(1,50,1): for b in range(1,50,1): for c in range(1,50,1): if a+b+c ...

  4. 如何在高并发分布式系统中生成全局唯一Id(转)

    http://www.cnblogs.com/heyuquan/p/global-guid-identity-maxId.html 又一个多月没冒泡了,其实最近学了些东西,但是没有安排时间整理成博文, ...

  5. 简单通用JDBC辅助类封装

    哎,最近很好久没在博客园写点东西了,由于工作的原因,接触公司自己研发的底层orm框架,偶然发现该框架在调用jdbc操作的时候参考的是hibernate 里面的SimpleJdbcTemplate,这里 ...

  6. 从0开始学Java——eclipse下运行servlet程序警告:Setting property 'source' to 'org.eclipse.jst.jee.server:类名' did not find a matching property.

    在使用Eclipse 创建第一个 Servlet之后,并且配置好了tomcat,然后Run on server的之后,提示标题所示错误: 警告: [SetContextPropertiesRule]{ ...

  7. 《深入理解计算机系统》 Chapter 7 读书笔记

    <深入理解计算机系统>Chapter 7 读书笔记 链接是将各种代码和数据部分收集起来并组合成为一个单一文件的过程,这个文件可被加载(货被拷贝)到存储器并执行. 链接的时机 编译时,也就是 ...

  8. Linux(9.21-9.27)学习笔记

    一.Vim的基本操作. Normal模式下 1.h 键 向左移动光标   2.  j  键  向下移动光标   3. k 键 向上移动光标 4. l键  向右移动光标 5.x 键  删除光标所在位置的 ...

  9. 20145222黄亚奇《Java程序设计》实验四实验报告

    20145222<Java程序设计>第四次实验报告 实验四 Android环境搭建 实验内容 1.搭建Android环境 2.运行Android 3.修改代码,能输出学号 实验步骤 搭建A ...

  10. 如何自学 Android 编程?

    最近知乎上有网友问我怎么自学Android,其实说实在的,我学的也一塌糊涂,当然在学习过程也积累了一些知识,对于以前没接触过Android的朋友,或者刚入门Android 的朋友,这篇文章作为入门,那 ...