Til the Cows Come Home POJ
来源:互联网 发布:游戏数据加密算法 编辑:程序博客网 时间:2024/06/05 07:00
题目:https://vjudge.net/problem/POJ-2387
好久没写博客了,算自己懒吧。
最短路入门题,自己练习用SPFA实现了下,不需要判负环也写上了。WA了好几次,忘记判断重边了。
代码:C++
//SPFA#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#include <set>#include <map>using namespace std;const int maxn = 2000 + 10;const int INF = 2147483647;map<int, map<int, int> > pic;int T, n;queue<int> q;bool inq[maxn];int cnt[maxn];int dis[maxn];void init(){ memset(cnt, 0, sizeof(cnt)); memset(inq, false, sizeof(inq)); for(int i = 1; i <= n; i++) { dis[i] = INF; } dis[1] = 0;}bool SPFA(){ q.push(1); inq[1] = true; cnt[1]++; while(!q.empty()) { int cur = q.front(); q.pop(); inq[cur] = false; for(map<int, int>::iterator iter = pic[cur].begin(); iter!=pic[cur].end(); iter++) { if(dis[cur]!=INF && dis[cur] + iter->second < dis[iter->first]) { dis[iter->first] = dis[cur] + iter->second; if(!inq[iter->first]) { q.push(iter->first); cnt[iter->first]++; inq[iter->first] = true; //判断负环 if(cnt[iter->first] > n) { return false; } } } } } return true;}int main(){ scanf("%d%d", &T, &n); init(); while(T--) { int x, y, w; scanf("%d%d%d", &x, &y, &w); if(!pic[x].count(y)) { pic[x][y] = w; pic[y][x] = w; } else { pic[x][y] = min(w, pic[x][y]); pic[y][x] = min(w, pic[y][x]); } } SPFA(); printf("%d\n", dis[n]); return 0;}
阅读全文
0 0
- POJ 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- Poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- POJ-2387-Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- 《黑客与画家》读书笔记
- Error creating bean with name 'itemController': Injection of resource dependencies failed; nested ex
- I
- 写 SQL 时常犯的 10 个错误
- JS中getElementById()、getElementsByName()、getElementsByTagName() 的区别和应用
- Til the Cows Come Home POJ
- 七月英语——安生
- NYOJ 236-心急的C小加(贪心,水题)
- 前m大的数
- 什么是EJB?EJB是基于哪些技术实现的?
- Android内存泄露及解决方法总结
- 漫步最优化十九——封闭算法
- 关注民生民情——华北水利水电大学“情艺”国情社情调查
- 【BZOJ3555】【Ctsc2014】企鹅QQ