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. php报错日志:PHP Deprecated:Automatically populating $HTTP_RAW_POST_DATA is deprecated

    前几天将线上php服务升级到5.6.x版本后,php-error.log报出错误:PHP Deprecated: Automatically populating $HTTP_RAW_POST_DAT ...

  2. Android中的IMEI

    国际移动设备识别码(IMEI:International Mobile Equipment Identification Number)是区别移动设备的标志,储存在移动设备中,可用于监控被窃或无效的移 ...

  3. VMWare安装苹果操作系统OS X

    项目要求做一些简单的苹果开发尝试,由于苹果的各种开发要求在Macintosh机上进行,可是项目不值得为一次简单的尝试付出过多的购机费,所以只能另辟蹊径,跟别人学学怎么在虚拟机里面搞: http://j ...

  4. zabbix(sql注入判断脚本)

    zabbix(sql注入判断脚本) #-*-coding:utf-8-*- # code by anyun.org import urllib import re def getHtml(url): ...

  5. Microsoft Visual Studio 正忙

    简介:Microsoft Visual Studio 正忙,Microsoft Visual Studio 正在等待内部操作完成.如果经常在正常使用的情况下遇到此延迟, 请向Microsoft报告此情 ...

  6. C中的预编译宏定义

     可以用宏判断是否为ARC环境 #if _has_feature(objc_arc) #else //MRC #endif C中的预编译宏定义 -- 作者: infobillows 来源:网络 在将一 ...

  7. CentOS 6.5 安装Nginx 1.7.4

    一.安装准备 首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库,这些依赖库主要有g++.gcc.openssl-devel.pcre-devel和zlib ...

  8. swift第二季高级语法

    一.类和结构体 二.属性 三.方法 四.下标 五.继承和扩展 六.初始化和反初始化

  9. 记录毕业论文 LanguageTool 二次开发时用到的网站

    LanguageTool Development LanguageTool Supported Languages Share your knowledge about LT - LanguageTo ...

  10. IT男的”幸福”生活"系列暂停更新通知

    首先谢谢博客园,这里给了我很多快乐.更给了大家一个学习的好地方. 在这几天更新过程中,看到了很多哥们的关注,在这里我谢谢你们,是你们给了我动力,是你们又一次给了我不一样的幸福. 在续5中我已回复了,博 ...