题意

题目链接

问从\(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?(负环)的更多相关文章

  1. POJ3621Sightseeing Cows[01分数规划 spfa(dfs)负环 ]

    Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9703   Accepted: 3299 ...

  2. POJ 3259 Wormholes (判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Descripti ...

  3. POJ 3259 Wormholes(最短路,判断有没有负环回路)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24249   Accepted: 8652 Descri ...

  4. Poj 3259 Wormholes(spfa判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...

  5. POJ 3259 Wormholes( bellmanFord判负环)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36425   Accepted: 13320 Descr ...

  6. POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】

    链接: http://poj.org/problem?id=3259 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  7. POJ3259 Wormholes —— spfa求负环

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  8. UVA11090 Going in Cycle!! [spfa负环]

    https://vjudge.net/problem/UVA-11090 平均权值最小的回路 为后面的做个铺垫 二分最小值,每条边权减去他,有负环说明有的回路平均权值小于他 spfa求负环的时候可以先 ...

  9. 【洛谷P3385】模板-负环

    这道题普通的bfs spfa或者ballen ford会T 所以我们使用dfs spfa 原因在于,bfs sfpa中每个节点的入队次数不定,退出操作不及时,而dfs则不会 既然,我们需要找负环,那么 ...

随机推荐

  1. MySQL索引(六)

    一.什么是索引 索引就像一本书的目录一样,如果在字段上建立索引,那么以索引为列的查询条件时可以加快查询的速度,这个就是MySQL优化的重要部分 二.创建主键索引 整个表的每一条记录的主键值在表内都是唯 ...

  2. Java执行Shell脚本“No such file or directory” (win->Linux)异常的可能原因

    转自:http://blog.csdn.net/zlpdaisy/article/details/6134314 用Runtime.getRuntime().exec()方法执行Linux的一个She ...

  3. Java之IO(十二)CharArrayReader和CharArrayWriter

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7082668.html 1.前言 本章介绍字符数组流,作用和ByteArrayInputStream字节数组流相同 ...

  4. IDEA里运行程序时出现Failed to execute goal org.scala-tools:maven-scala-plugin:2.15.2:compile(default) on Project DataFusion:wrap:org.apache.commons...错误的解决办法(图文详解)

    不多说,直接上干货! 问题详情 比如我们习惯在IDEA里打包用mvn clean package 在Scala IDEA for eclipse里出现mvn clean compile assembl ...

  5. C/C++ -- Gui编程 -- Qt库的使用 -- 理解主窗体构造函数

    MyWidget做父窗体 MyWidget的构造函数中可以手动添加组件 Ui::MyWidget存放子部件 Ui::MyWidget执行setupUi()函数为子部件开辟空间,指定父窗体 MyWidg ...

  6. Redis笔记(一):Redis安装教程

    Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. Redis是目前应用最广泛的内存数据存储技术,相比之前的Me ...

  7. 假如想要建设一个能承受500万PV/每天的网站,服务器每秒要处理多少个请求才能应对?

    假如想要建设一个能承受500万PV/每天的网站,服务器每秒要处理多少个请求才能应对?如何计算? 1.PV是什么:PV是page view的简写.PV是指页面的访问次数,每打开或刷新一次页面,就算做一个 ...

  8. eclipse maven插件创建maven项目

    1.下载好maven压缩包http://maven.apache.org/ ,解压后放到想要安装的目录,如E:/server/maven,然后配置好maven环境变量,找到maven安装目录下conf ...

  9. 高级网络功能(Docker支持的网络定制配置)

    网络的高级知识,包括网络的启动和配置参数.DNS的使用配置.容器访问和端口映射的相关实现. 在一些具体场景中,Docker支持的网络定制配置,通过Linux命令来调整.补充.甚至替换Docker默认的 ...

  10. C#(Winform)中button的Enable=false和visible的区别

    Enable:可用性 visible:可见性(不是visiable哦,没有这个单词) 如果设置了button或其他控件的Enable为false,还是能够看到控件,只是控件是灰色的,而且点不了: 但设 ...