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. UI:这段时间的小总结

    关于 UITAbleView 的重用机制 参考1  参考2   参考3 关于 UITableViewController  的知识来自博客 参考1  参考2  参考3 总结 一个工程的基本框架的规范写 ...

  2. MySQL事务处理和锁机制

    事务处理和并发性 1.1 基础知识和相关概念 1 )全部的表类型都可以使用锁,但是只有 InnoDB 和 BDB 才有内置的事务功能. 2 )使用 begin 开始事务,使用 commit 结束事务, ...

  3. c++中智能输出文件

    首先我们要为每一时间步,设置一个文件名: ] = "; itoa(time,timestr,); std::string s; s += timestr; std::string path ...

  4. redis-在乌班图下设置自动启动

    一.修改redis.conf 1.打开后台运行选项,默认情况下,Redis不在后台运行: daemonize yes 2.配置log文件地址,默认使用标准输入,即打印在命令行终端 的窗口上 logfi ...

  5. CSS Layout

    fontline-heightcolormarginpaddingbordertext-alignbackground widthheightfloatcleardisplay 定位属性 属 性 描 ...

  6. wpa_supplicant 中的wpa_supplicant.conf

    主要记录下wep加密相关的配置文件的写法. network={ ssid="static-wep-test2" key_mgmt=NONE wep_key0= //密钥索引为2, ...

  7. 【转】larbin主要代码说明

    转自:http://blog.csdn.net/s030702614/article/details/5683928 1. 主函数: int main (int argc, char *argv[]) ...

  8. ghostDoct 使用 (转 http://www.cnblogs.com/RockyMyx/archive/2010/04/20/Project-Route-Using-GhostDoc.html)

    一.简介 GhostDoc是Visual Studio的一个免费插件,可以为开发人员自动生成XML格式的注释文档. 二.下载 需要的朋友可以去这里下载,填个Email地址就可以下了:GhostDoc下 ...

  9. iOS开发——动画总结OC篇&所有常用动画总结

    所有常用动画总结 先来装下B,看不懂没关系,其实我也看不懂-

  10. Swift基础使用方法(Swift开发之中的一个)

    昨晚苹果公布了新一代编程语言Swift,官方提供了一个iBook的说明文档.有须要的能够看下.地址:mt=11" target="_blank">https://i ...