POJ 3259 Wormholes——spfa判负环
来源:互联网 发布:javascript 创建元素 编辑:程序博客网 时间:2024/06/10 19:09
注意不要把正边覆盖!
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <queue>#include <utility>using namespace std;typedef pair<int, int> P;const int INF = 0x3f3f3f3f;const int maxn = 505;int T, N, M, W, dis[maxn], vis[maxn], num[maxn];vector<P> G[maxn];bool spfa() { for (int i = 1; i <= N; i++) dis[i] = INF, vis[i] = 0, num[i] = 0; dis[1] = 0, vis[1] = 1, num[1] = 1; queue<int> q; q.push(1); while (!q.empty()) { int u = q.front(); q.pop(); vis[u] = 0; for (int i = 0; i < G[u].size(); i++) { int v = G[u][i].first, val = G[u][i].second; if (dis[v] > dis[u] + val) { dis[v] = dis[u] + val; if (!vis[v]) { vis[v] = 1; q.push(v); num[v]++; if (num[v] > N) return true; } } } } return false;}int main() { scanf("%d", &T); while (T--) { scanf("%d %d %d", &N, &M, &W); for (int i = 1; i <= N; i++) G[i].clear(); for (int i = 1; i <= M; i++) { int u, v, cost; scanf("%d %d %d", &u, &v, &cost); G[u].push_back(make_pair(v, cost)); G[v].push_back(make_pair(u, cost)); } for (int i = 1; i <= W; i++) { int u, v, val; scanf("%d %d %d", &u, &v, &val); G[u].push_back(make_pair(v, -val)); } if (spfa()) printf("YES\n"); else printf("NO\n"); }}
阅读全文
1 0
- POJ 3259 Wormholes——spfa判负环
- 【POJ 3259】Wormholes 【spfa判负环】
- poj 3259--Wormholes 【spfa判负环】
- POJ 3259-Wormholes-SPFA判负环
- POJ 3259 Wormholes SPFA判负环
- poj 3259 Wormholes【spfa判负环】
- POJ 3259 Wormholes spfa判负环
- poj 3259 Wormholes //SPFA
- poj 3259 Wormholes SPFA
- POJ 3259 Wormholes(SPFA)
- Wormholes - POJ 3259 spfa
- poj 3259 Wormholes(spfa)
- POJ 3259:Wormholes 【SPFA】
- Wormholes POJ 3259【SPFA】
- poj 3259-- Wormholes(SPFA)
- POJ-3259 Wormholes(SPFA)
- Poj 3259 Wormholes ( SPFA
- POJ 3259 Wormholes spfa
- C# 从TTF文件加载自定义字体
- session:web会话管理的方式
- C# bitmap 灰度图像 Pattle 问题
- java基础数据结构分析
- Java基础部分第二节
- POJ 3259 Wormholes——spfa判负环
- HDU6201 2017 transaction transaction transaction 树形DP?
- 分割车牌
- RxJava——简介
- HDU3555 Bomb
- Java分页技术的实现(一)
- 搜狗 2017年秋招编程题 两点的距离
- 多模字符串匹配算法之AC自动机—原理与实现
- 关于Eclipse tomcat添加