题目链接: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的更多相关文章

  1. POJ 2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K ...

  2. 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 ...

  3. POJ.2387 Til the Cows Come Home (SPFA)

    POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...

  4. 【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, ...

  5. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  6. 【LeetCode】299. Bulls and Cows 解题报告(Python)

    [LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  7. 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 ...

  8. POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

    传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Acce ...

  9. 怒学三算法 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 ...

随机推荐

  1. jQuery 事件委派

    /******************************************************************/ $(function(){ //live()事件委派,后续添加 ...

  2. PAT_A1048#Find Coins

    Source: PAT A1048 Find Coins (25 分) Description: Eva loves to collect coins from all over the univer ...

  3. 2019秋第一次Java学习总结

    本周Java学习总结: 知识点总结: 1.Java中程序的执行步骤 使用Javac将一个.Java源文件编译成.class文件 使用Java可以执行一个*.class文件 2.&&与& ...

  4. 8.1_springboot2.x之Actuator应用监控

    1.监管端点测试 引入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...

  5. 净心诀---python3生成器进阶

    列表推导式 把需要用一个函数写成的小功能,利用一行表达式完成 例子: l = [1,2,3,4,5] # 所有的偶数都放到新的列表中 # 正常函数 def Lst(): li = [] for i i ...

  6. 如何在react中实现一个倒计时组件

    倒计时组件 import React, { Component } from 'react' import $ from 'jquery' import "../../css/spellTE ...

  7. android应用的资源

    应用资源可以分为两大类: 1.无法直接访问的原生资源,保存在asset目录下. 2.可以通过R资源清单类访问的资源,保存在res目录下. 资源的类型以及存储方式: android要求在res目录下用不 ...

  8. 文件IO 例子

    例子1: 测试最多打开多少个文件 #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> # ...

  9. kafka?kafaka! kafka...

    kafka?kafaka! Kafka... kafka是什么? 答:Kafka是由Apache软件基金会开发的一个开源流处理平台,由Scala和Java编写.Kafka是一种高吞吐量的分布式发布订阅 ...

  10. 解决MySQL登录密码正确却提示错误-1045的方法

    MySQL密码正确却无法本地登录-1045 Access denied for user 'root'@'localhost' (using password:YES MySQL密码正确却无法本地登录 ...