Poj(3259),SPFA,判负环
题目链接:http://poj.org/problem?id=3259
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 44090 | Accepted: 16203 |
Description
While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ's farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..N, M (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.
As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .
To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.
Input
Line 1 of each farm: Three space-separated integers respectively: N, M, and W
Lines 2..M+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path.
Lines M+2..M+W+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.
Output
Sample Input
2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8
Sample Output
NO
YES
Hint
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.
#include <stdio.h>
#include<iostream>
#include <queue>
#include <string.h> using namespace std; #define INF 0x3f3f3f3f int n,m,t; struct Edge
{
int v;
int w;
int next;
} edge[]; int NE = ; int dis[];
int head[];
bool vis[];
int cnt[]; void add(int u,int v,int d)
{
edge[NE].v = v;
edge[NE].w = d;
edge[NE].next = head[u];
head[u]= NE++;
} bool SPFA()
{
for(int i=; i<=n; i++)
{
dis[i] = INF;
vis[i] = false;
cnt[i] = ;
} dis[] = ;
vis[] = true;
cnt[] = ;
queue<int> Q;
Q.push(); while(!Q.empty())
{
int u=Q.front();
Q.pop();
vis[u]=false;
for(int i=head[u]; i!=-; i=edge[i].next)
{ if(dis[edge[i].v]>dis[u]+edge[i].w)
{
dis[edge[i].v] = dis[u] + edge[i].w;
if(!vis[edge[i].v])
{
vis[edge[i].v] = true;
Q.push(edge[i].v);
if(++cnt[edge[i].v]>n)
return true;
}
}
}
}
return false;
} int main()
{
//freopen("input.txt","r",stdin);
int cases;
scanf("%d",&cases);
while(cases--)
{ NE = ;
memset(head,-,sizeof(head));
scanf("%d%d%d",&n,&m,&t); for(int i=; i<m; i++)
{
int a,b,d;
scanf("%d%d%d",&a,&b,&d);
add(a,b,d);
add(b,a,d);
} for(int i=; i<t; i++)
{
int a,b,d;
scanf("%d%d%d",&a,&b,&d);
add(a,b,-d);
} if(SPFA())
printf("YES\n");
else printf("NO\n");
}
return ;
}
Poj(3259),SPFA,判负环的更多相关文章
- Wormholes POJ - 3259 spfa判断负环
//判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> # ...
- POJ - 3851-Wormholes(SPFA判负环)
A friend of yours, an inventor, has built a spaceship recently and wants to explore space with it. D ...
- POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...
- Poj 3259 Wormholes(spfa判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...
- poj 2049(二分+spfa判负环)
poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...
- poj 1364 King(线性差分约束+超级源点+spfa判负环)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14791 Accepted: 5226 Description ...
- spfa判负环
bfs版spfa void spfa(){ queue<int> q; ;i<=n;i++) dis[i]=inf; q.push();dis[]=;vis[]=; while(!q ...
- 2018.09.24 bzoj1486: [HNOI2009]最小圈(01分数规划+spfa判负环)
传送门 答案只保留了6位小数WA了两次233. 这就是一个简单的01分数规划. 直接二分答案,根据图中有没有负环存在进行调整. 注意二分边界. 另外dfs版spfa判负环真心快很多. 代码: #inc ...
- BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划
BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划 更清真的题面链接:https://files.cnblogs.com/files/winmt/merchant%28zh_ ...
- [P1768]天路(分数规划+SPFA判负环)
题目描述 “那是一条神奇的天路诶~,把第一个神犇送上天堂~”,XDM先生唱着这首“亲切”的歌曲,一道猥琐题目的灵感在脑中出现了. 和C_SUNSHINE大神商量后,这道猥琐的题目终于出现在本次试题上了 ...
随机推荐
- 证明 logX < X 对所有 X > 0 成立
题目取自:<数据结构与算法分析:C语言描述_原书第二版>——Mark Allen Weiss 练习1.5(a) 证明下列公式: logX < X 对所有 X > ...
- Effective C++ 3.资源管理
//条款13:以对象管理资源 // 1.C++程序中最常使用的资源就是动态分配内存,并且还包括文件描述器,互斥锁,GDI对象.数据库连接.网络socket等.不管哪一种资源,当不再使用的时候必须将其归 ...
- JS小练习 留言功能
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 将activity变成dialog风格
其实很简单: 只是把activity的theme设置成dilog形式的: <activity android:name=".ActivityPackage.DatePickerActi ...
- HDU 4064 Carcassonne(插头DP)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4064 Problem Description Carcassonne is a tile-based ...
- 2013年各大小IT公司待遇
2013年各大小IT公司待遇(初版 摘自好网)本人西电硕士,根据今年找工作的情况以及身边同学的汇总,总结各大公司的待遇如下,吐血奉献给各位学弟学妹,公司比较全,你想去的公司不在这里面,基本上是无名 ...
- linux c
#include <stdio.h>#include <string.h>#include <strings.h> int main(){ char buf[ ...
- 转:V$SQL,V$SQLAREA,V$SQLTEXT
V$SQL*表用于查看Shared SQL Area中SQL情况 V$SQLTEXT V$SQLTEXT用途很简单,就是用来查看完整的SQL语句,V$SQL和V$SQLAREA只能显示1000 byt ...
- qsort函数用法(转)
qsort函数用法 qsort 功 能: 使用快速排序例程进行排序 用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(c ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON AddNoiseWhite
zw版[转发·台湾nvp系列Delphi例程]HALCON AddNoiseWhite unit Unit1;interfaceuses Windows, Messages, SysUtils, Va ...