【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 ...
随机推荐
- appium 定位弹出框时报错
今天在做APP自动化时,发现定位弹出框无法定位,无奈,百度去找.发现了一篇不错的博客,故转载过来,供大家参考.后续会验证这个方法的可行性. 本博客转自:http://blog.csdn.net/qq7 ...
- [参考]C# JSON字符串序列化与反序列化
C#将对象序列化成JSON字符串 public string GetJsonString() { List<Product> products = new List<Product& ...
- JEECG树(TreeGrid)字段的扩展
转载:https://blog.csdn.net/huangzirong822/article/details/38400817 之前使用jeecg集成框架做过一个项目 这里说说框架中树(tree)字 ...
- SQL中Truncate语法
转自:http://www.studyofnet.com/news/555.html 本文导读:删除表中的数据的方法有delete,truncate, 其中TRUNCATE TABLE用于删除表中的所 ...
- sanic连接mongo
方法一: #没有密码,就是没有用户和用户密码 settings={"MOTOR_URI":"mongodb://127.0.0.1:27017/zzy"} ap ...
- find out the installed and runing tomcat version in Linux
To find out the Tomcat version, find this file – version.sh for *nix or version.bat for Windows. Thi ...
- 微信小程序--学习的案例
一.「程序秀」 「程序秀」这个小程序非常特别,因为它是一个可以帮助别人开发小程序的小程序.它以「即速应用」这个无需代码一键生成微信小程序的开发工具为基础,为广大开发爱好者提供小程序开发的最新资讯和培训 ...
- Html+css编写太阳星系
我们都知道太阳系是以太阳为中心的,和所有受到太阳的引力约束天体的集合体.包括八大行星(由离太阳从近到远的顺序:水星.金星.地球.火星.木星.土星.天王星.海王星),而我用html和css所写的就是八大 ...
- JQ基本
jQuery的入口函数: 特点:1. 等着DOM结构渲染完毕即可执行内部代码,不必等到所有外部资源加载完毕,jQuery帮我们完成了封装. 2. 相当于原生js中的DOMContentLoaded. ...
- zic2xpm - 将 ZIICS 象棋片段 (chess pieces) 转换为 XBoard (XPM/XIM) 片段的工具。
总览 SYNOPSIS zic2xpm file1 [file2 ...] 描述 zic2xpm 将一个或多个 ZIICS 片段文件转换为 XBoard 可用的格式.如果你给出一个以上的文件名,小心同 ...