POJ - 3851-Wormholes(SPFA判负环)
During his first voyages, he discovered that the universe is full of wormholes created by some alien race. These wormholes allow one to travel to places far, far away, but moreover, they can also send you to times long ago or in the distant future.
Having mapped these wormholes and their respective end points, you and your friend boldly decide to board his spaceship and go to some distant place you'd like to visit. Of course, you want to arrive at your destination as early as possible. The question is: what is this earliest arrival time?
Input
All coordinates are integers with absolute values smaller than or equal to 10 000 and no two points are the same.
Note that, initially, the time is zero, and that tunneling through a wormhole happens instantly. For simplicity, the distance between two points is deffned as their Euclidean distance (the square root of the sum of the squares of coordinate differences) rounded up to the nearest integer. Your friend's spaceship travels at speed 1.
Output
Sample Input
2
0 0 0 100 0 0
2
1 1 0 1 2 0 -100 -2
0 1 0 100 1 0 -150 10
0 0 0 10 0 0
1
5 0 0 -5 0 0 0 0
Sample Output
-89
10 代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>
#define Inf 0x3f3f3f3f const int maxn=1e5+;
typedef long long ll;
using namespace std; struct Edge
{
int u,v,w;
int next;
}edge[];
int head[],vis[],cnt[],dis[],tot;
void init()
{
memset(head,-,sizeof(head));
memset(cnt,,sizeof(cnt));
memset(vis,,sizeof(vis));
memset(dis,Inf,sizeof(dis));
}
void add(int u,int v,int w)
{
edge[tot].u=u;
edge[tot].v=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
return;
}
int n,m,k;
bool SPFA(int s)
{
dis[s]=;
cnt[s]=;
vis[s]=true;
queue<int>q;
q.push(s);
while(!q.empty())
{
int now=q.front();
q.pop();
vis[now]=false;
for(int i=head[now];i!=-;i=edge[i].next)
{
if(dis[now]+edge[i].w<dis[edge[i].v])
{
dis[edge[i].v]= dis[now]+edge[i].w;
int y=edge[i].v;
cnt[y]=cnt[now]+;
if(cnt[y]>n)
{
return true;
}
if(vis[y]==false)
{
vis[y]=true;
q.push(y);
}
}
}
}
return false; } int main()
{
int T;
cin>>T;
while(T--)
{
tot=;
init();
scanf("%d%d%d",&n,&m,&k);
int u,v,w;
for(int t=;t<m;t++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
for(int t=;t<k;t++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,-w);
}
if(SPFA())
{
puts("YES");
}
else
{
puts("NO");
} } }
POJ - 3851-Wormholes(SPFA判负环)的更多相关文章
- POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...
- poj 3621 二分+spfa判负环
http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...
- [poj3259]Wormholes(spfa判负环)
题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环. 两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...
- Wormholes POJ 3259(SPFA判负环)
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- POJ 3259 Wormholes (判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Descripti ...
- POJ3259 :Wormholes(SPFA判负环)
POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 While exploring his many ...
- POJ 3259 Wormholes ( SPFA判断负环 && 思维 )
题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...
- POJ 3259 Wormholes( bellmanFord判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36425 Accepted: 13320 Descr ...
- 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,有多组数据. 首先根 ...
随机推荐
- [Err] 126 - Incorrect key file for table '/tmp/#sql_1cdc_0.MYI'; try to repair it
网上给出的原因倾向于MySQL的tmpdir的临时目录磁盘空间不足,如果检查确实时空间不足,请调整量tmpdir和slave_load_tmpdir,然后重启mysql服务.调整的方法是 locate ...
- Python稳居编程语言榜首,看完这篇总结,你就明白为什么要学它了
最近,网上流传一组<人工智能实验教材>的图片,照片火起来的原因是教材是为幼儿园的小朋友们设计的! Python被列入小学.初高中教材已不是新鲜事,现在又成功“入侵”了幼儿园,对此有网友调侃 ...
- Linux入门-基本概念
本文主要介绍linux基础概念介绍,对基本概念了解后,更好入门. 这里主要介绍一下几个概念 什么是linux GNU项目和自由软件基金会 linux发行版 什么是linux 也许大家都已经知道,L ...
- 033_go语言中的打点器
代码演示 package main import "fmt" import "time" func main() { ticker := time.NewTic ...
- Android Studio局部管理器
1.LinearLayout(线性布局) 该布局将其中的View子控件按照水平或者垂直方向排列.但是需要注意不管是水平还是竖直,对应的每一行或列都只能放一个控件. 线性布局两种排法: 从左到右:and ...
- 程序员必须了解!IntelliJ IDEA 2020.2的新增功能
IDEA 因为之前破解过,所以家里的电脑都是19版本的,用的也比较顺手,也就一直懒得去动他,但是,程序猿的好奇心可能真的挺重,猎奇心里,在网上也看到了很多关于2020版本的idea的各种好处,于是,闲 ...
- artifactdescriptorexception:Failed to read artifact descriptor for xxx:jar ”
在Eclipse中执行Maven的install命令时,报“Failed to read artifact descriptor for xxx:jar ”的错误.这可能是在下载过程中文件出现错误.或 ...
- 2020-07-06:一个6亿的表a,一个3亿的表b,通过外间tid关联,你如何最快的查询出满足条件的第50000到第50200中的这200条数据记录
福哥答案2020-07-06:表a和表b的字段都是id和tid,数据类型都是int.查询结果顺序上以 表a 为准.1.JOIN.SELECT * FROM a JOIN b ON a.tid = b. ...
- Mac 从睡眠恢复后没有声音的问题
重启能解决,不想重启,因为不想重置当前工作状态 换个办法是: 杀掉coreaudiod进程,然后系统会把他自动拉起,然后就ok了.
- C#连接Oracle数据库,通过EF自动生成与数据库表相关的实体类
C#连接Oracle数据库,通过EF自动生成与数据库表相关的实体类 ps:如需转载,请在转载文章明显处,i标注作者和原文地址 一.准备条件 需要自己电脑上已经安装了Oracle数据库,并且已经创建了相 ...