题意

题目链接

问从\(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. flask-mysqldb安装时EnvironmentError: mysql_config not found

    安装时候的日志如下: sh: : mysql_config: not found Traceback (most recent call last): File , in <module> ...

  2. jmeter 中使用ServerAgen链接超时可能出错的原因之一ip不对

    因为我要压测的服务器是需要使用跳板机转发链接的,所以我开始用的是跳板机的IP+ServerAgen端口,发现连不通,实际上应该使用ServerAgen所在服务器的IP,如果:

  3. Windows平台下Git服务器搭建--------gitblit

    Windows(server)平台下Git服务器搭建 第一步:下载Java,安装,配置环境变量. 第二步:下载Gitblit.下载地址:http://www.gitblit.com/ 第三步:解压缩下 ...

  4. C++ 实现Biginteger

    网上C++版Biginteger参差不齐,一下子没有找到一个令人满意Biginteger,最近用c++改写了一下C#版 BigInteger,可以用于RSA大素数的生成,分享给大家.也请大家批评指正改 ...

  5. EF基础知识小记六(使用Code First建模自引用关系,常用于系统菜单、文件目录等有层级之分的实体)

    日常开发中,经常会碰到一些自引用的实体,比如系统菜单.目录实体,这类实体往往自己引用自己,所以我们必须学会使用Code First来建立这一类的模型. 以下是自引用表的数据库关系图: ok,下面开始介 ...

  6. css设置:图片文字等不能被选择

    -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none;

  7. 【jQuery源码】select方法

    /** * select方法是Sizzle选择器包的核心方法之一,其主要完成下列任务: * 1.调用tokenize方法完成对选择器的解析 * 2.对于没有初始集合(即seed没有赋值)且是单一块选择 ...

  8. LogCat里的错误提示 FATAL EXCEPTION: main

    程序一运行闪退. 原因为包冲突,将冲突的包删除即可.

  9. HUE配置文件hue.ini 的hdfs_clusters模块详解(图文详解)(分HA集群和非HA集群)

    不多说,直接上干货! 我的集群机器情况是 bigdatamaster(192.168.80.10).bigdataslave1(192.168.80.11)和bigdataslave2(192.168 ...

  10. Centos7下安装mysql5.6需要注意的点

    1.自带的Mariadb和mysql冲突需要卸载. 2.原先安装过的mysql没有卸载干净会导致安装失败. 3.mysql文件夹权限需要给够,my.cnf也是一样. 4.安装过程中如果出现的其他问题很 ...