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 ...
随机推荐
- POJ 1275 Cashier Employment(差分约束)
http://poj.org/problem?id=1275 题意 : 一家24小时营业的超市,要雇出纳员,需要求出超市每天不同时段需要的出纳员数,午夜只需一小批,下午需要多些,希望雇最少的人,给出每 ...
- 使用NSURLSession实现断点续传
在sb中创建按钮,并且连线到.m文件中
- Android USB Host与HID通讯 (一)
去年9月份来到现在的公司,接到新公司的第一个项目就是Android USB Host与HID通讯,当时也什么都不懂,就拿着google的api 开发指南 (http://developer.andro ...
- 弱安全协议探测工具-sslciphercheck
SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为 网络通信提供安全及数据完整性的一种安全协议.TLS ...
- ArcGIS Engine Style文件操作
对于一个GISer来说,地图,符号这些都应该有着比别人更深刻的理解和认识,作为平台软件都会提供一套自己的符号库,符号库里面根据类别和种类进行区分,因为点,线,面的自然存在和固有属性是不肯能让你用面状符 ...
- Sass占位符选择器`%`
摘录自Understanding placeholder selectors. @extend @extend让你能够在多个选择器中通过继承的方式共享一段样式: .icon { transition: ...
- 加载网络映射盘中的assembly失败的处理办法
错误症状: 1.{"未能加载文件或程序集“file://*****”或它的某一个依赖项.不支持操作. (异常来自 HRESULT:0x80131515)":"file:/ ...
- MySQL purge log简单吗
MySQL purge log简单吗? 简单: purge log的功能很简单,purge master logs to 指定一个binlog,然后删除之前的文件系统上的binlog文件,并更新bin ...
- CodeForces 400
A - Inna and Choose Options Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d &a ...
- ☀【css】透明
让IE浏览器支持RGBA颜色http://www.hujuntao.com/web/css/ie-browser-support-for-rgba-color.html 透明纯色背景 backgrou ...