1715: [Usaco2006 Dec]Wormholes 虫洞
1715: [Usaco2006 Dec]Wormholes 虫洞
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 501 Solved: 278
[Submit][Status][Discuss]
Description
John在他的农场中闲逛时发现了许多虫洞。虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前)。John的每个农场有M条小路(无向边)连接着N (从1..N标号)块地,并有W个虫洞。其中1<=N<=500,1<=M<=2500,1<=W<=200。 现在John想借助这些虫洞来回到过去(出发时刻之前),请你告诉他能办到吗。 John将向你提供F(1<=F<=5)个农场的地图。没有小路会耗费你超过10000秒的时间,当然也没有虫洞回帮你回到超过10000秒以前。
Input
* Line 1: 一个整数 F, 表示农场个数。
* Line 1 of each farm: 三个整数 N, M, W。
* Lines 2..M+1 of each farm: 三个数(S, E, T)。表示在标号为S的地与标号为E的地中间有一条用时T秒的小路。
* Lines M+2..M+W+1 of each farm: 三个数(S, E, T)。表示在标号为S的地与标号为E的地中间有一条可以使John到达T秒前的虫洞。
Output
* Lines 1..F: 如果John能在这个农场实现他的目标,输出"YES",否则输出"NO"。
Sample Input
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
YES
HINT
Source
题解:很明显是找负权回路,可是我还一直没写过,直到今天第一次写spfa找负权回路——bfs的spfa只要判断下每个节点的入队次数,只要大于N,就可以判断有负环
/**************************************************************
Problem:
User: HansBug
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ type
point=^node;
node=record
g,w:longint;
next:point;
end;
var
i,j,k,l,m,n,t:longint;
a:array[..] of point;
b,c,g,e:array[..] of longint;
d:array[..] of longint;
procedure add(x,y,z:longint);
var p:point;
begin
new(p);p^.g:=y;p^.w:=z;p^.next:=a[x];a[x]:=p;
end; function spfa(x:longint):boolean;
var f,r:longint;p:point;
begin
fillchar(e,sizeof(e),);
fillchar(b,sizeof(b),);
fillchar(g,sizeof(g),);
for i:= to n do c[i]:=maxlongint div ;
f:=;r:=;g[x]:=;d[]:=x;c[x]:=;b[x]:=;e[x]:=;
while f<>r do
begin
p:=a[d[f]];
while p<>nil do
begin
if (e[p^.g]=) or (c[p^.g]>(c[d[f]]+p^.w)) then
begin
e[p^.g]:=;
c[p^.g]:=c[d[f]]+p^.w;
if g[p^.g]= then
begin
inc(b[p^.g]);
if b[p^.g]>n then exit(true);
g[p^.g]:=;
d[r]:=p^.g;
r:=r mod +;
end;
end;
p:=p^.next;
end;
g[d[f]]:=;f:=f mod +;
end;
exit(false);
end;
begin
readln(t);
while not(eof) do
begin
readln(n,m,t);
for i:= to n do a[i]:=nil;
for i:= to m do
begin
readln(j,k,l);
add(j,k,l);add(k,j,l);
end;
for i:= to t do
begin
readln(j,k,l);
add(j,k,-l);
end;
if spfa() then writeln('YES') else writeln('NO');
end;
end.
1715: [Usaco2006 Dec]Wormholes 虫洞的更多相关文章
- bzoj 1715: [Usaco2006 Dec]Wormholes 虫洞 -- spfa判断负环
1715: [Usaco2006 Dec]Wormholes 虫洞 Time Limit: 5 Sec Memory Limit: 64 MB 注意第一次加边是双向边第二次是单向边,并且每次询问前数 ...
- BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞
Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N ...
- BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞 DFS版SPFA判负环
Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N ...
- bzoj 1715: [Usaco2006 Dec]Wormholes 虫洞【spfa判负环】
tag是假的,用了及其诡异的方法判负环 正权无向边和负权有向边的图 #include<iostream> #include<cstdio> #include<cstrin ...
- BZOJ1715: [Usaco2006 Dec]Wormholes 虫洞
1715: [Usaco2006 Dec]Wormholes 虫洞 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 475 Solved: 263[Sub ...
- 洛谷2850 【Usaco2006 Dec】虫洞Wormholes SPFA
问题描述 John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N (从1..N ...
- [USACO2006 DEC] Wormholes
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1715 [算法] 用SPFA判定负环是否存在即可 时间复杂度 : O(N ^ 2) [ ...
- 【SPFA判断负环】BZOJ1715- [Usaco2006 Dec]Wormholes 虫洞
[题目大意] 判断一张图中是否存在负环. [思路] dfs版SPFA. #include<bits/stdc++.h> using namespace std; struct edge { ...
- BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 [后缀数组]
1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1017 Solved: ...
随机推荐
- webview 设置编码
WebSettings settings = webView.getSettings(); // 设置页面编码 settings.setDefaultTextEncodingName("ut ...
- asp.net客户端IP跟踪
/// <summary> /// 获取客户端IP地址(无视代理) /// </summary> /// <returns>若失败则返回回送地址</retur ...
- json-lib之复杂数据类型的转换
在json字符串转java bean时,一般的对象,可以直接转,如:一个学生类,属性有姓名.年龄等 public class Student{ private String sname; privat ...
- GitHub客户端发布托管代码
初试GitHub及客户端使用 突然想分享代码,于是记起来曾几何时有人提到过GitHub这个东西,于是便各种百度,注册申请了一个账号,下载了windows客户端,全英文网站就连新手教程也是全英的,现在想 ...
- html 一般标签 常用标签 表格
body的属性: bgcolor 页面背景色 background 背景壁纸.图片 text 文字颜色 top ...
- C++编程练习(4)----“实现简单的栈的链式存储结构“
如果栈的使用过程中元素数目变化不可预测,有时很小,有时很大,则最好使用链栈:反之,如果它的变化在可控范围内,使用顺序栈会好一些. 简单的栈的链式存储结构代码如下: /*LinkStack.h*/ #i ...
- 谨慎能捕千秋蝉(一)——XSS
最近在研读<白帽子讲web安全>和<Web前端黑客技术揭秘>,为了加深印象,闲暇之时做了一些总结. 下面是书中出现的一些专有词汇: POC(Proof Of Concept): ...
- executssql 函数的每一句代码的意思
Public Function Executesql(ByVal sql As String, Msgstring As String) As ADODB.Recordset Dim cnn As A ...
- 矩阵求逆的几种方法总结(C++)
矩阵求逆运算有多种算法: 伴随矩阵的思想,分别算出其伴随矩阵和行列式,再算出逆矩阵: LU分解法(若选主元即为LUP分解法: Ax = b ==> PAx = Pb ==>LUx = Pb ...
- STL中sort排序算法第三个参数_Compare的实现本质
关于C++ STL vector 中的sort排序算法有三种自定义实现,它们本质上都是返回bool类型,提供给sort函数作为第三个参数. 重载运算符 全局的比较函数 函数对象 我认为从实现方式看,重 ...