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则不会 既然,我们需要找负环,那么 ...
随机推荐
- jmeter-server中启动后端口总是不断在变化
1.首先找到这个文件打开: 2.修改两个地方如图: 第一个:server_port=xxxx 第二个:server.rmi.localport=xxxx 3.重启jmeter-server,这是在li ...
- day 65 crm(2) admin源码解析,以及简单的仿造admin组件
前情提要: crm中的admin组件重写. 一:admin的autodiscover 作用:实现扫面该项目中的所有的admin 1:执行顺序-> 按照注册的顺序执行 二:单例模式 1:普通案例的 ...
- postgresql 脏读-dirtied
共享缓冲区 在内存中读取或写入数据总是比在任何其他介质上更快.数据库服务器还需要用于快速访问数据的内存,无论是READ还是WRITE访问.在PostgreSQL中,这被称为"共享缓冲区&qu ...
- PostgreSQL 数据库错误代码解释
PostgreSQL 服务器发出的所有消息都赋予了五个字符 的错误代码, 这些代码遵循 SQL 的 "SQLSTATE" 代码的习惯.需要知道发生了什么错误条件的应用通常应该测试错 ...
- ubuntu 16.04 docker下安装klee环境
手动构建docker映象: 从github上获取klee源码 git clone https://github.com/klee/klee.git cd klee 使用存储库根目录中dockerfil ...
- centos安装python与jdk
安装python #压缩包安装 [root@china ~]# yum -y install zlib* Loaded plugins: fastestmirror, refresh-packagek ...
- (转)mysql自增列导致主键重复问题分析
mysql自增列导致主键重复问题分析... 原文:http://www.cnblogs.com/cchust/p/3914935.html 前几天开发童鞋反馈一个利用load data infile ...
- Spring Security构建Rest服务-1201-Spring Security OAuth开发APP认证框架之实现服务提供商
实现服务提供商,就是要实现认证服务器.资源服务器. 现在做的都是app的东西,所以在app项目写代码 认证服务器: 新建 ImoocAuthenticationServerConfig 类,@Ena ...
- Android Library项目发布到JCenter最简单的配置方法
前沿 网上的步骤看起来实在太麻烦,gituhb上偶然间看到的一个项目,经过实际验证确实可行.github连接:https://github.com/xiaopansky/android-library ...
- Android 开发工具类 06_NetUtils
跟网络相关的工具类: 1.判断网络是否连接: 2.判断是否是 wifi 连接: 3.打开网络设置界面: import android.app.Activity; import android.cont ...