POJ 2387 Til the Cows Come Home(最短路模板)
题目链接:http://poj.org/problem?id=2387
题意:有n个城市点,m条边,求n到1的最短路径。n<=1000; m<=2000
就是一个标准的最短路模板。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue> using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f3f;
int n, m;
struct node{
int to, cost;
node() {}
node(int a, int b) :to(a), cost(b) {}
};
vector<node> e[maxn];
int vis[maxn], f[maxn], dis[maxn];
void SPFA(int s)
{
for (int i = ; i < maxn; i++) {
vis[i] = ; f[i] = ;
dis[i] = INF;
}
dis[s] = ;
vis[s] = ; f[s]++;
queue<int>Q;
Q.push(s);
while (!Q.empty()) {
int t = Q.front(); Q.pop();
vis[t] = ;
for (int i = ; i < e[t].size(); i++) {
int tmp = e[t][i].to;
if (dis[tmp] > dis[t] + e[t][i].cost) {
dis[tmp] = dis[t] + e[t][i].cost;
if (!vis[tmp]) {
vis[tmp] = ;
Q.push(tmp);
if (++f[tmp] > n)return;
}
}
}
}
return;
}
int main()
{
ios::sync_with_stdio(false);
while (cin >> m >> n) {
for (int a, b, c, i = ; i <= m; i++) {
cin >> a >> b >> c;
e[a].push_back(node(b, c));
e[b].push_back(node(a, c));
}
SPFA();
cout << dis[n] << endl;
}
return ;
}
POJ 2387 Til the Cows Come Home(最短路模板)的更多相关文章
- POJ 2387 Til the Cows Come Home --最短路模板题
Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...
- 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 ...
- 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 ...
- POJ.2387 Til the Cows Come Home (SPFA)
POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- 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(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- 怒学三算法 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 ...
- 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 ...
随机推荐
- jQuery打飞机游戏
在线演示 本地下载
- CSS Specificity
CSS的specificity特性或称非凡性,它是一个衡量CSS值优先级的一个标准. 其实就是解决冲突,当同一个元素被CSS选择符选中,按照优先权取舍不同的CSS规则. specificity用一个四 ...
- 关于编码的发展演变:ASCII、GB2312、GBK、gb18030、Unicode、UTF-8
[1]ASCII 每个字符占据1bytes(字节),第一次以规范标准发表是在1967年,最后一次修订是在1986年.用二进制表示的话最高位必须为0(扩展的ASCII不在考虑范围内),因此ASCII只能 ...
- Cacti 加入多台主机带宽汇聚
前面我写了一个cacti加入主机带宽监控的博客.能够參考http://blog.csdn.net/dai451954706/article/details/35272465 .有时可能 ...
- Java练习 SDUT-1239_水仙花数
水仙花数 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花 ...
- DDoS攻击新趋势:海量移动设备成为新一代肉鸡
近期,阿里云安全团队观察到数十起大规模的应用层资源耗尽式DDoS攻击(应用层CC攻击).阿里云DDoS高防实现智能防护全程自动化检测并清洗,未对用户侧业务产生任何影响,这类攻击存在一些共同的特征,阿里 ...
- Python中json和eval的区别
>>> import json >>> s = '{"one":1,"two":2}' >>> json. ...
- HZOI 可怜与超市
网上搜不着,八成又是哪个学长留下的…… 因为考试第二题我们都好不容易才搞懂,学长有给我们扔了几道类似的题. 其实这道题思路挺好想的,就是一些细节还有复杂度比较难弄,好难调啊. 看到题的第一眼以为是树形 ...
- selenium webdriver学习(四)------------定位页面元素(转)
selenium webdriver学习(四)------------定位页面元素 博客分类: Selenium-webdriver seleniumwebdriver定位页面元素findElemen ...
- jasperReports 通过java导出各种格式报表,及javaweb项目整合显示各种报表
注意:编写此项目时,请先手动编写**.jrxml 或者用IReport生成**.jrxml或**.jasper 注:java导出报表与web项目显示报表方法其实是相同的,只不过有一句代码不同,如下 ...