【POJ】2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387
题意:求从1到n的最短路
题解:板子题。spfa。
代码:
#include<iostream>
#include<stack>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = 2e5+; vector< pair<int,int> > e[maxn]; int n,m;
int d[maxn],inq[maxn]; void init(){
for(int i = ; i < maxn; i++)
e[i].clear();
for(int i = ;i < maxn ; i++)
inq[i] = ;
for(int i = ; i < maxn ; i++)
d[i] = 1e9;
} int main(int argc, const char * argv[]) {
while(cin>>m>>n){
init();
int x,y,z;
for(int i = ; i < m ;i++){
cin>>x>>y>>z;
e[x].push_back(make_pair(y,z));
e[y].push_back(make_pair(x,z));
}
int s,t;
//cin>>s>>t;
s = ;
t = n;
queue<int>Q;
Q.push(s);d[s] = ;inq[s] = ;
while( !Q.empty() ){
int now = Q.front();
Q.pop();
inq[now] = ;
for(int i = ; i < e[now].size() ; i++){
int v = e[now][i].first;
if(d[v] > d[now] + e[now][i].second){
d[v] = d[now] + e[now][i].second;
if(inq[v] == )
continue;
inq[v] = ;
Q.push(v);
}
} }
if(d[t] == 1e9)
cout<<-<<endl;
else
cout<<d[t]<<endl;
}
return ;
}
看题没看仔细。。。一直n和m输入反了,还以为是去重的问题。搞了好久。。然后发现其实pair这样存储的模式不用考虑去重的问题。也算是有所收获吧。哎。心痛。。
【POJ】2387 Til the Cows Come Home的更多相关文章
- 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 (图论,最短路径)
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】1704 Georgia and Bob(Staircase Nim)
Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...
- 【POJ】1067 取石子游戏(博弈论)
Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 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(最短路 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 ...
随机推荐
- (转)VS2015基础 指定一个或多个项目执行 - 心少朴的博客
慈心积善融学习,技术愿为有情学.善心速造多好事,前人栽树后乘凉.我今于此写经验,愿见文者得启发. 这个解决方案下,有两个项目, 看到黑体的project了吗?它就是指定执行的项目. 这两 ...
- LintCode 汉诺塔
题目链接:https://www.lintcode.com/problem/tower-of-hanoi/description 题目大意 经典递归问题. 分析 由于是经典问题了,这里不讨论用递归实现 ...
- 剑指offer——53字符流中第一个只出现一次的字符
题目描述 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字符流中读出 ...
- Java DOM解析器
文档对象模型是万维网联盟(W3C)的官方推荐.它定义了一个接口,使程序能够访问和更新样式,结构和XML文档的内容.支持DOM实现该接口的XML解析器. 何时使用? 在以下几种情况时,应该使用DOM解析 ...
- 使用ProxyBroker构建代理池
import asyncio from proxybroker import Broker async def show(proxies): while True: proxy = await pro ...
- 常用css3属性的ie兼容查看
记录一下关于css3的各种常用属性对ie各版本浏览器的兼容程度: 最低可兼容ie7 最低可兼容ie8 最低可兼容ie9 最低可兼容ie10 position:fixed clip E:first-le ...
- python xlwt设置单元格的自定义背景颜色
我使用python 2.7和xlwt模块进行excel导出 我想设置我知道可以使用的单元格的背景颜色 style1 = xlwt.easyxf('pattern: pattern solid, for ...
- JavaScript小实例-文本循环变色效果
在现实生活中我们常常看到文字循环变色的效果,此效果不仅能让人们印象深刻,还提高了美观度,代码及注释如下: <!DOCTYPE html> <html> <head> ...
- java heap space以及jvisualvm.exe 工具
最近遇到了java heap space错误. 这个问题的原因,其实还是堆溢出了. 解决这个问题 1 首先我们考虑,在代码中哪里使用了较多的对象,但是又没有及时回收. 2 我们可以通过 jvisua ...
- Jenkins添加Windows自动化构建方案
一.为Jenkins添加Windows节点 这里需要填写远程工作目录,启动方法一项一定要选择"Launch agent via Java Web Start"一项,其它的保持默认. ...