A - Til the Cows Come Home
SPFA
算法采用图的存储结构是邻接表,方法是动态优化逼近法。算法中设立了一个先进先出的队列Queue用来保存待优化的顶点,优化时从此队列里顺序取出一个点w,并且用w点的当前路径D[W]去优化调整其它各点的路径值D[j],若有调整,即D[j]的值改小了,就将J点放入Queue队列以待继续进一步优化。反复从Queue队列里取出点来对当前最短路径进行优化,直至队空不需要再优化为止,此时D数组里就保存了从源点到各点的最短路径值
///////////////////////////////////////////////////////////////////////////
竟然一遍过......好神奇的感觉,写起来也简单了不少
#include<algorithm>
#include<queue>
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std; const int maxn = ;
const int oo = 0xfffffff; struct node
{
int y, len;
node(int y, int len):y(y), len(len){}
};
vector<node> G[maxn];
int v[maxn]; void Spfa(int s)
{
queue<int> Q; Q.push(s); while(Q.size())
{
s = Q.front();Q.pop(); int len = G[s].size();
for(int i=; i<len; i++)
{
node q = G[s][i];
if(v[s]+q.len < v[q.y])
{
v[q.y] = v[s]+q.len;
Q.push(q.y);
}
}
}
} int main()
{
int T, N; while(scanf("%d%d", &T, &N) != EOF)
{
int i, a, b, len; for(i=; i<T; i++)
{
scanf("%d%d%d", &a, &b, &len);
G[a].push_back(node(b, len));
G[b].push_back(node(a, len));
} for(i=; i<=N; i++)
v[i] = oo;
v[] = ; Spfa(); printf("%d\n", v[N]); for(i=; i<=N; i++)
G[i].clear();
} return ;
}
A - Til the Cows Come Home的更多相关文章
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- Til the Cows Come Home(最短路)
Til the Cows Come Home Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I ...
- POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37662 Accepted ...
- POJ 2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
- 怒学三算法 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 ...
- 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 ...
- 3386/1752: [Usaco2004 Nov]Til the Cows Come Home 带奶牛回家
3386/1752: [Usaco2004 Nov]Til the Cows Come Home 带奶牛回家 Time Limit: 1 Sec Memory Limit: 128 MBSubmit ...
- (Dijkstra) POJ2387 Til the Cows Come Home
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 81024 Accepted ...
- POJ 2387 Til the Cows Come Home 【最短路SPFA】
Til the Cows Come Home Description Bessie is out in the field and wants to get back to the barn to g ...
- 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 ...
随机推荐
- [中级] 有效删除URL中的index.php
如果你刚接触CI不久又或者刚刚研读CI的使用手册的话,关于如何有效删除URL中index.php以使URL看起来更友好美观的问题,可能是你面对的第一个较为复杂的问题!本贴不是原创,而是一个各种意见的综 ...
- [转]Delphi调用cmd的两种方法
delphi调用cmd的两种方法vars:string;begins:='cmd.exe /c '+edit1.Text+' >c:\1.txt';winexec(pchar(s),sw_hid ...
- windows API 统计系统字体
最近工作中遇到一个需求,需要统计当前系统中包含的所有字体.在网上逛了一圈后发现了EnumFontFamiliesEx这个API好像就可以实现这个功能.这里将自己对这个API的理解做一个记录,算是对这块 ...
- nginx1.8+php5.6.10 服务器编译安装备忘2015-06
又要重新装一台阿里云服务器.开始想用脚本,但发现脚本的程序版本都比较低 还是手动编译最新版本 开始前 更新服务器到最新版本 #yum makecache #yum update //分区挂数据盘 # ...
- 1s延时程序
#include <reg52.h>sbit P1_0 = P1^0;void Delay(); // 下面引用时一定要和这里的大小写一致否则会有警告或错误 void Main(){whi ...
- eclipse 比较好的插件
tomcat 插件 egit github 插件 subeclipse 插件 Properties Editor Properties Editor 编辑java的属性文件,并可以自动存盘为Unico ...
- Java中传参的值传递和引用传递问题(转)
今天遇到了一个java程序,需要用参数来返回值(虽然最后用另一种方法实现了),在网上看到这样一篇文章,很受启发. 本文章来自于http://hi.baidu.com/xzhilie/blog/item ...
- 转:SSDB:快速取代redis的nosql
原文来自于:http://hao.jobbole.com/ssdb%EF%BC%9A%E5%BF%AB%E9%80%9F%E5%8F%96%E4%BB%A3redis%E7%9A%84nosql/ S ...
- WPF会重写Windows GUI的历史吗?
原文地址:http://tech.it168.com/zx/2007-09-15/200709141320653.shtml 你可能对微软的.NET框架3.0版本的最近的一次更新感到有点奇怪.主版本指 ...
- 又是干货,这个春节感觉吃饱了。各方面---RHCS集群理论
什么是集群? 集群是一组(>2)相互独立的,通过高速网络互联的计算机组成的集合.群集一般可以分为科学集群,负载均衡集群,高可用性集群三大类. 科学集群是并行计算的基础.它对外就好象一 ...