UVAlive6800The Mountain of Gold?(负环)
题意
问从\(0\)出发能否回到\(0\)且边权为负
Sol
先用某B姓算法找到负环,再判一下负环上的点能否到\(0\)
#include<bits/stdc++.h>
#define chmax(x, y) (x = (x > y ? x : y))
#define chmin(x, y) (x = (x < y ? x : y))
#define Pair pair<int, int>
#define MP make_pair
#define fi first
#define se second
using namespace std;
const int MAXN = 2e6 + 10, INF = 1e9 + 10;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, num, dis[MAXN], vis[MAXN], gg[MAXN];
struct Edge {
int u, v, w;
}E[MAXN];
vector<int> v[MAXN];
void AddEdge(int x, int y, int z) {
v[x].push_back(y);
E[++num] = (Edge) {x, y, z};
}
bool dfs(int x) {
// printf("%d\n", x);
gg[x] = 1;
if(vis[x] != -1) return vis[x];
if(x == 1) return vis[x] = 1;
bool flag = 0;
for(int i = 0; i < v[x].size(); i++) {
int to = v[x][i];
if(!gg[to] && dfs(to)) {flag = 1; break;}
}
return vis[x] = flag;
}
bool SPFA() {
dis[1] = 0;
for(int i = 1; i < N; i++)
for(int j = 1; j <= M; j++)
chmin(dis[E[j].v], dis[E[j].u] + E[j].w);
for(int i = 1; i <= M; i++) {
int x = E[i].u, y = E[i].v;
if((dis[y] > dis[x] + E[i].w) && dfs(y)) return 1;
}
return 0;
}
void init() {
for(int i = 1; i <= N; i++) v[i].clear();
memset(vis, -1, sizeof(vis));
memset(dis, 0x3f, sizeof(dis));
memset(gg, 0, sizeof(gg));
num = 0;
}
void solve(int Case) {
N = read(); M = read();
init();
for(int i = 1; i <= M; i++) {
int x = read() + 1, y = read() + 1, z = read();
AddEdge(x, y, z);
}
printf("Case #%d: ", Case); puts(SPFA() ? "possible" : "not possible");
}
int main() {
//freopen("a.in", "r", stdin);
for(int T = read(), i = 1; i <= T; solve(i++));
return 0;
}
UVAlive6800The Mountain of Gold?(负环)的更多相关文章
- POJ3621Sightseeing Cows[01分数规划 spfa(dfs)负环 ]
Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9703 Accepted: 3299 ...
- POJ 3259 Wormholes (判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Descripti ...
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
- Poj 3259 Wormholes(spfa判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...
- POJ 3259 Wormholes( bellmanFord判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36425 Accepted: 13320 Descr ...
- POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】
链接: http://poj.org/problem?id=3259 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ3259 Wormholes —— spfa求负环
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- UVA11090 Going in Cycle!! [spfa负环]
https://vjudge.net/problem/UVA-11090 平均权值最小的回路 为后面的做个铺垫 二分最小值,每条边权减去他,有负环说明有的回路平均权值小于他 spfa求负环的时候可以先 ...
- 【洛谷P3385】模板-负环
这道题普通的bfs spfa或者ballen ford会T 所以我们使用dfs spfa 原因在于,bfs sfpa中每个节点的入队次数不定,退出操作不及时,而dfs则不会 既然,我们需要找负环,那么 ...
随机推荐
- webpack快速入门——集中拷贝静态资源
工作中会有一些已经存在但在项目中没有引用的图片资源或者其他静态资源(比如设计图.开发文档), 这些静态资源有可能是文档,也有可能是一些额外的图片.项目组长会要求你打包时保留这些静态资源, 直接打包到制 ...
- java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
spring的项目中有时候会报错:java.lang.IllegalStateException: BeanFactory not initialized or already closed - ca ...
- 使用 find 命令实现高级排除需求
使用 find 命令实现高级排除需求 Linked 关于 find 命令,本博客介绍过 atime,ctime,mtime 介绍过 --exec 参数. 介绍这些的基本需求是进行文件管理.事实上,基于 ...
- POJ 1111
#include<iostream> #define MAXN 30 using namespace std; char _m[MAXN][MAXN]; bool mark[MAXN][M ...
- XmlDocument 避免XXE
string xml2 = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\&quo ...
- error "Can only specify query options (orderby, where, take, skip) after last navigation" when fetching a List<string>
Question I use OData v3 and WCF in visual studio 2012. I want to return List<string> using the ...
- python 把一文件包含中文的字符写到另外文件乱码 UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position
报错的代码是: file2 = open('target.txt','w')for line in open('test.txt'): file2.write(line)原因:文件编码不一致导致解决方 ...
- C# 点击打开浏览器
System.Diagnostics.Process.Start("网址");//默认浏览器打开网页System.Diagnostics.Process.Start(@" ...
- Redis笔记(三):Redis常用命令
连接测试 连接本地服务器 语法 $ redis-cli 实例 启动 redis 客户端,打开终端并输入命令 redis-cli.该命令会连接本地的 redis 服务. $redis-cli redis ...
- 13-hadoop-入门程序
通过之前的操作, http://www.cnblogs.com/wenbronk/p/6636926.html http://www.cnblogs.com/wenbronk/p/6659481.ht ...