HDU 3259 Wormholes
题意:就是给你一个n,m,t n代表有多少个点。m代表有多少个双向的边 t代表的是虫洞。如今要你判读是否还能够穿越到过去的点
虫洞的意思是给你的边是单向的,而且是负权值(输入的时候是正数)
思路:能否够穿越回过去的点,即有没有负环。果断套用模板,dijkstra算法不能检測负环
AC代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
#define maxn 520
const int INF = 0x3fffffff;
struct Edge
{
int from,to,dist;
};
struct BellmanFord
{
int n,m;
vector<Edge> edges;
vector<int> G[maxn];
bool inq[maxn];
int d[maxn];
int p[maxn];
int cnt[maxn];
Edge e;
void init(int n)
{
this->n=n;
for(int i=0;i<n;i++)
G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int dist)
{
edges.push_back((Edge){from,to,dist});
m=edges.size();
G[from].push_back(m-1);
}
bool negativeCycle()
{
queue<int >Q;
memset(inq,0,sizeof(inq));
memset(cnt,0,sizeof(cnt));
for(int i=0;i<n;i++)
{
d[i]=INF;
inq[0]=true;
Q.push(i);
}
d[0] = 0;
while(!Q.empty())
{
int u=Q.front();
Q.pop();
inq[u]=false; for(int i=0;i<G[u].size();i++)
{
Edge& e=edges[G[u][i]];
if(d[e.to]>d[u]+e.dist)
{
d[e.to]=d[u]+e.dist;
p[e.to]=G[u][i];
if(!inq[e.to])
{
Q.push(e.to);
inq[e.to]=true;
if(++cnt[e.to]>n)
return true;
}
}
} }
return false;
}
};
int main()
{
int a,b,c,i,node,m,t,case1,k;
bool j;
scanf("%d",&case1);
while(case1--)
{
scanf("%d %d %d",&node,&m,&t);
if(node==0&&m==0)break;
BellmanFord tu;
tu.init(node);
for(i=0;i<m;i++)
{
scanf("%d %d %d",&a,&b,&c);
a--;b--;
tu.AddEdge(a,b,c);
tu.AddEdge(b,a,c);
}
for(k=0;k<t;k++)
{
scanf("%d %d %d",&a,&b,&c);
a--;
b--;
tu.AddEdge(a,b,-c);
}
j=tu.negativeCycle();
if(j)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
HDU 3259 Wormholes的更多相关文章
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- 最短路(Bellman_Ford) POJ 3259 Wormholes
题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...
- POJ 3259 Wormholes (Bellman_ford算法)
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- poj 3259 Wormholes
题目连接 http://poj.org/problem?id=3259 Wormholes Description While exploring his many farms, Farmer Joh ...
- poj - 3259 Wormholes (bellman-ford算法求最短路)
http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...
- POJ 1860 Currency Exchange + 2240 Arbitrage + 3259 Wormholes 解题报告
三道题都是考察最短路算法的判环.其中1860和2240判断正环,3259判断负环. 难度都不大,可以使用Bellman-ford算法,或者SPFA算法.也有用弗洛伊德算法的,笔者还不会SF-_-…… ...
- POJ 3259 Wormholes(最短路径,求负环)
POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
- POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】
链接: http://poj.org/problem?id=3259 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
随机推荐
- about js
function: javascript jquery modernizr yepnope code organization requirejs backbonejs http://blog.csd ...
- Chapter 17. Objects and Inheritance(对象与继承)
javascript面向对象编程有几个层面: 1: 单一对象 (covered in Layer 1: Single Objects) 2: 对象之间的 prototype (described i ...
- ListView 长按拖动会变黑的解决方案
在ListView的xml文件中添加这个属性: android:cacheColorHint="#00000000"
- UVALive - 3401 Colored Cubes
好久没写解题回顾了.主要是没什么时间,但是还是一直在刷题,图论刷了70%的知识点,不过感觉长进不是很大,所以觉得还是得一步步来,最近还是先从刘汝佳大白书把前面基础章节刷完然后再决定以后的训练方式吧. ...
- iOS开发UI篇—UITabBarController生命周期(使用storyoard搭建)
iOS开发UI篇—UITabBarController生命周期(使用storyoard搭建) 一.UITabBarController在storyoard中得搭建 1.新建一个项目,把storyb ...
- easyui源码翻译1.32--TreeGrid(树形表格)
前言 扩展自$.fn.datagrid.defaults.使用$.fn.treegrid.defaults重写默认值对象.下载该插件翻译源码 树形表格用于显示分层数据表格.它是基于数据表格.组合树控件 ...
- 各种HTTP错误消息含义
错误代码 错误消息 400 无法解析此请求. 401.1 未经授权:访问由于凭据无效被拒绝. 401.2 未经授权: 访问由于服务器配置倾向使用替代身份验证方法而被拒绝. 401.3 未经授权:访问由 ...
- Attribute的一个列子
其实在博客中也写过这个东西,也介绍过它的原理,原理很简单,就是在运行的时候通过反射拦截获取一些信息,但是我在写程序的时候几乎没用过,可能是自己接触的还不够多,也许是因为自己接触的功能不算复杂往往几句代 ...
- Good vs Evil
Good vs Evil Description Middle Earth is about to go to war. The forces of good will have many battl ...
- Android What is Application
本文转自:http://www.cnblogs.com/elleniou/archive/2012/05/16/2502661.html Application和Activity,Service一样是 ...