【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 ...
随机推荐
- Ubuntu 16.04系统上修改Docker镜像的存储路径 (转)
转自:https://blog.csdn.net/qihongchao/article/details/80651492 dba专门挂了一个硬盘让我放项目(测试环境)因为系统空间比较小,希望把dock ...
- 浅谈C/C++中的static和extern关键字
static是C++中常用的修饰符,它被用来控制变量的存贮方式和可见性.extern "C"是使C++能够调用C写作的库文件的一个手段,如果要对编译器提示使用C的方式来处理函数的话 ...
- Java-框架-Dubbo:Dubbo
ylbtech-Java-框架-Dubbo:Dubbo Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成 ...
- Java学习之集合(List接口)
List特有的常见方法:有一个共性特点:都可以操作角标 1.添加 void add(int index, E element); void addAll(int index, collection& ...
- SDUTOJ 2498 数据结构实验之图论十一:AOE网上的关键路径
题目链接:http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/2498.html 题目大意 略. 分析 ...
- node.js 中的 fs (文件)模块
记录 fs 模块的方法及使用 1. fs.stat 获取文件大小,创建时间等信息 // 引入 fs 模块 const fs = require('fs'); fs.stat('01.fs.js', ( ...
- 剑指offer——40字符串的排列
题目描述 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入描述: 输 ...
- Win7环境下VS2010配置Cocos2d-x-2.1.4最新版本的开发环境(亲测)
写这篇博客时2D游戏引擎Cocos2d-x的最新版本为2.1.4,记得很久以前使用博客园博主子龙山人的一篇博文<Cocos2d-x win7+vs2010配置图文详解(亲测)>成功配置 ...
- Mysql 命令行下建立存储过程
建立存储过程的sql如下: CREATE PROCEDURE proc_variable () BEGIN DECLARE dec_var_ VARCHAR(100); DECLARE rep_nu ...
- kafka相关业务必会操作命令整理
参考:https://kafka.apache.org 服务相关命令 1.启动/停止zk > bin/zookeeper-server-start.sh config/zookeeper.pro ...