poj 3259 Wormholes 负权边最短路
来源:互联网 发布:2017淘宝客推广技巧 编辑:程序博客网 时间:2024/06/05 10:10
Problem:
判断是否存在负权环
Solution:
利用spfa算法
#include <iostream>#include<cstdio>#include <vector>#include <queue>#include <cstring>using namespace std;const int maxn = 510;const int INF = 0x3fffffff;struct Node{ int e, w; Node(int ne, int nw) : e(ne),w(nw){} Node(){}};vector<vector<Node> > G;int updateTimes[maxn];//判断是否有环int dist[maxn];bool spfa(int v, int n){//输入源点, 节点个数 for(int i = 1; i <= n; ++i) dist[i] = INF; dist[v] = 0; queue<int> q; q.push(v); memset(updateTimes ,0,sizeof(updateTimes)); while(!q.empty()){ int s = q.front(); q.pop(); for(int i = 0; i < G[s].size(); ++i) { int e = G[s][i].e; if(dist[e] > dist[s] + G[s][i].w){ dist[e] = dist[s] + G[s][i].w; q.push(e); ++updateTimes[e]; if(updateTimes[e] >= n) return true; } } } return false;}int f,n,m,w;int main(){ scanf("%d",&f); while(f--){ scanf("%d%d%d",&n,&m,&w); G.clear(); G.resize(n+5); int s,e,t; for(int i = 0; i < m; ++ i) { scanf("%d%d%d", &s, &e, &t); G[s].push_back(Node(e,t)); G[e].push_back(Node(s,t)); } for(int i = 0;i < w; ++i) { scanf("%d%d%d", &s, &e, &t); G[s].push_back(Node(e,-t)); } if(spfa(1, n)) printf("YES\n"); else printf("NO\n"); } return 0;}
0 0
- poj 3259 Wormholes 负权边最短路
- poj 3259 Wormholes 最短路
- poj 3259 Wormholes 最短路
- POJ Wormholes 3259(最短路)
- poj 3259 Wormholes 最短路
- POJ 3259 Wormholes(最短路)
- POJ 3259 Wormholes(最短路Bellman_Ford)
- poj 3259 Wormholes (最短路 Bellma_ford)
- poj 3259 Wormholes (负权最短路,SPAF)
- poj 3259 Wormholes 图论 最短路 判负环
- POJ 3259 Wormholes(最短路)
- POJ 3259Wormholes(最短路bellman)
- POJ 3259 Wormholes(floyd求负环,最短路)
- POJ-3259-Wormholes [最短路][BellmanFord]
- POJ-3259 Wormholes【单源最短路判负环】
- poj 3259 -- Wormholes(最短路,负环)
- poj 3259 Wormholes(最短路+spfa+判负回路)
- poj 3259 Wormholes (spfa求最短路)
- 使用opencv的Haar训练自己的人脸分类器
- attribute 和 parameter的区别
- Markdown常用语法,MarkDownPad
- 在Windows下快速搭建SVN服务器 VisualSVN
- WritingStrategy
- poj 3259 Wormholes 负权边最短路
- SQL直接操作excel表(查询、导入、插入)
- C/C++生成可执行文件过程
- 运用Altium.Designer.v9.Summer09布线的几个原则
- 视觉slam参考资料
- centOS7下实践查询版本/CPU/内存/硬盘容量等硬件信息
- 206. Reverse Linked List
- 后台UI的选择与介绍
- Java爬虫技术