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

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

Sample Output

90

理论上的效率应该是Dijsktra > SPFA > Bellman_Ford,但是前两者我用了vector,影响了效率,导致贝尔曼是最快的,迪杰斯特拉其次。
 #include <iostream>
#include <vector>
#include <cstdio>
#include <queue>
using namespace std; const int SIZE = ;
const int INF = 0x2fffffff;
bool S[SIZE];
int N,D[SIZE];
struct Node
{
int vec,cost;
};
struct comp
{
bool operator ()(int & a,int & b)
{
return D[a] > D[b];
}
};
vector<Node> G[SIZE];
priority_queue <int,vector<int>,comp> QUE; void dijkstra(int);
void relax(int,int,int);
int main(void)
{
int t,from;
Node temp; scanf("%d%d",&t,&N);
while(t --)
{
scanf("%d%d%d",&from,&temp.vec,&temp.cost);
G[from].push_back(temp);
swap(from,temp.vec);
G[from].push_back(temp);
}
dijkstra(N);
printf("%d\n",D[]); return ;
} void dijkstra(int s)
{
fill(D,D + SIZE,INF);
D[s] = ;
S[s] = true;
QUE.push(s); while(!QUE.empty())
{
int cur = QUE.top();
int len = G[cur].size();
S[cur] = true;
QUE.pop();
for(int i = ;i < len;i ++)
relax(cur,G[cur][i].vec,G[cur][i].cost);
if(cur == )
return ;
}
} void relax(int from,int to,int cost)
{
if(D[to] > D[from] + cost)
{
D[to] = D[from] + cost;
if(!S[to])
QUE.push(to);
}
}

Dijkstra

 #include <iostream>
#include <cstdio>
using namespace std; const int INF = 0x5fffffff;
const int SIZE = ;
bool UPDATE;
int D[SIZE];
int N,E;
struct Node
{
int from,to,cost;
}Edge[SIZE * ]; void Bellman_Ford(int);
void relax(int,int,int);
int main(void)
{
int t;
Node temp; scanf("%d%d",&t,&N);
while(t --)
{
scanf("%d%d%d",&temp.from,&temp.to,&temp.cost);
Edge[E ++] = temp;
swap(temp.from,temp.to);
Edge[E ++] = temp;
}
Bellman_Ford(N);
printf("%d\n",D[]); return ;
} void Bellman_Ford(int s)
{
fill(D,D + SIZE,INF);
D[s] = ; for(int i = ;i < N - ;i ++)
{
UPDATE = false;
for(int j = ;j < E;j ++)
relax(Edge[j].from,Edge[j].to,Edge[j].cost);
if(!UPDATE)
return ;
}
} void relax(int from,int to,int cost)
{
if(D[to] > D[from] + cost)
{
D[to] = D[from] + cost;
UPDATE = true;
}
}

Bellman-Ford

 #include <iostream>
#include <cstdio>
#include <queue>
using namespace std; const int SIZE = ;
const int INF = 0x5fffffff;
int N,D[SIZE];
bool IN_QUE[SIZE];
struct Node
{
int to,cost;
};
vector<Node> G[SIZE]; void spfa(int);
bool relax(int,int,int);
int main(void)
{
int t,from;
Node temp; scanf("%d%d",&t,&N);
while(t --)
{
scanf("%d%d%d",&from,&temp.to,&temp.cost);
G[from].push_back(temp);
swap(from,temp.to);
G[from].push_back(temp);
}
spfa(N);
printf("%d\n",D[]); return ;
} void spfa(int s)
{
int vec,cost;
queue<int> que;
fill(D,D + SIZE,INF);
D[s] = ;
IN_QUE[s] = true;
que.push(s); while(!que.empty())
{
int cur = que.front();
int len = G[cur].size();
IN_QUE[cur] = false;
que.pop(); for(int i = ;i < len;i ++)
{
vec = G[cur][i].to;
cost = G[cur][i].cost;
if(relax(cur,vec,cost) && !IN_QUE[vec])
{
IN_QUE[vec] = true;
que.push(vec);
}
}
}
} bool relax(int from,int to,int cost)
{
if(D[to] > D[from] + cost)
{
D[to] = D[from] + cost;
return true;
}
return false;
}

SPFA

怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)的更多相关文章

  1. POJ 2387 Til the Cows Come Home(dijkstra裸题)

    题目链接:http://poj.org/problem?id=2387 题目大意:给你t条边(无向图),n个顶点,让你求点1到点n的最短距离. 解题思路:裸的dijsktra,注意判重边. 代码: # ...

  2. (简单) POJ 2387 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 ...

  3. POJ 2387 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 ...

  4. 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 ...

  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

    题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K ...

  7. POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)

    原题链接:Til the Cows Come Home 题目大意:有  个点,给出从  点到  点的距离并且  和  是互相可以抵达的,问从  到  的最短距离. 题目分析:这是一道典型的最短路径模版 ...

  8. POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

    传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Acce ...

  9. 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 ...

随机推荐

  1. mysql 字段操作

    1.添加字段 ALTER TABLE lucky_user ADD COLUMN id_type TINYINT NOT NULL DEFAULT '0' COMMENT "0: 普通用户, ...

  2. VC中常用的宏[转]

    我们在VS环境中开发的时候,会遇到很多宏定义,这些宏可以应用到代码中,或用于编译.工程选项等设置,总之是我们开发中必不可少的工具,有必要做一个总结.有些宏是C/C++定义的,有些宏是VC环境预定义的. ...

  3. Android vector标签 PathData 画图超详解

    SVG是一种矢量图格式,是Scalable Vector Graphics三个单词的首字母缩写.在xml文件中的标签是<vector>,画出的图形可以像一般的图片资源使用,例子如下: &l ...

  4. HTML5 progress元素的样式控制、兼容与实例

    一.progress元素基本了解 基本UIprogress元素属于HTML5家族,指进度条.IE10+以及其他靠谱浏览器都支持.如下简单code: <progress>o(︶︿︶)o< ...

  5. c#获取或修改配置文件

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...

  6. intval()和(int)转换使用与区别

    <?php echo "<br/>数值强制转换:"; $string="2a"; $string1=intval($string); echo ...

  7. 从Windows 服务器通过sync向Linux服务器定时同步文件

    本文解决的是Windows 下目录及文件向Linux同步的问题,Windows向 Windows同步的请参考:http://www.idcfree.com/article-852-1.html 环境介 ...

  8. CSS3+Js制作的一款响应式导航条

    今天制作了一个响应式导航条,能够自动随着不同的屏幕分辨率或浏览器窗口大小的不同而改变导航条的样式,这里主要用到的就是CSS3的Media Query.具体可以查看浅谈响应式布局这篇文章,这里就不花费大 ...

  9. codeforces Gym 100187A A. Potion of Immortality

    A. Potion of Immortality Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1001 ...

  10. C# 的时间戳转换

    /// <summary> /// 时间戳转为C#格式时间 /// </summary> /// <param name="timeStamp"> ...