题意

题目链接

问从\(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. elasticsearch中 refresh 和flush区别

    elasticsearch中有两个比较重要的操作:refresh 和 flush refresh操作 当我们向ES发送请求的时候,我们发现es貌似可以在我们发请求的同时进行搜索.而这个实时建索引并可以 ...

  2. 3.修改更新源sources.list,提高软件下载安装速度(2017.04.05)

    2017年4月5日再次更新源 1.切换到root用户(如果已经是root用户就直接看第二步) dnt@HackerKali:~$ su 密码: 2.用文本编辑器打开sources.list,手动添加下 ...

  3. 【10】JMicro微服务-API网关

    如非授权,禁止用于商业用途,转载请注明出处作者:mynewworldyyl 往下看前,建议完成前面1到9小节 1. Api网关基本特性: Api网关作为对外网提供服务的基本入口,地位类似于NGINX, ...

  4. 53.storm简介

    一.简介 1.storm是twitter开源的一个分布式的实时计算系统,用于数据实时分析,持续计算,分布式RPC等等. 官网地址:http://storm-project.net 源码地址:https ...

  5. (转)用Python写堡垒机项目

    原文:https://blog.csdn.net/ywq935/article/details/78816860 前言 堡垒机是一种运维安全审计系统.主要的功能是对运维人员的运维操作进行审计和权限控制 ...

  6. [转]SQL的主键和外键约束

    SQL的主键和外键的作用: 外键取值规则:空值或参照的主键值. (1)插入非空值时,如果主键表中没有这个值,则不能插入. (2)更新时,不能改为主键表中没有的值. (3)删除主键表记录时,你可以在建外 ...

  7. ASP.NET5使用FaceBook登录

    原版教程 使用VS2015创建Web应用: 此教程使用的是FaceBook账号登录,需要添加相关的类,打开Nuget: 搜索Microsoft.AspNet.Authentication.Facebo ...

  8. javac后期需要重点阅读的类

    (1)Annotate (300行) Enter annotations on symbols. Annotations accumulate in a queue,which is processe ...

  9. js便签笔记(1)——说说HTMLCollection、NodeList以及NamedNodeMap

    介绍 在js的dom操作中,除了常用的document.html**Element之外,还有三个集合对象,即HTMLCollection.NodeList以及NamedNodeMap.试看以下操作: ...

  10. springboot-19-整合dubbox

    springboot 整合dubbox 1, 没了,,, 2, 安装zookeeper 可见: http://www.cnblogs.com/wenbronk/p/6636926.html 2.1 下 ...