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,有多组数据. 首先根 ...
随机推荐
- 00-java语言概述
1.基础常识软件:即一系列按照特定顺序组织的计算机数据和指令的集合.分为:系统软件 和 应用软件 系统软件:windows , mac os , linux ,unix,android,ios,... ...
- 如何从appstore下架自己家的app
本文主要讲如何下架appstore上边的app,我看了好多百度的,但是大部分说的都是14年左右的 ,我这边说一下最新的 1.用开发者帐号登陆到iTunes Connect 2.在iTunes Conn ...
- mysql删除数据库提示mysql Error dropping database (can't rmdir './db'...
1.执行ps aux | grep mysql,查看mysql的data目录,比如结果是--datadir=/var/lib/mysql. 2.进入data目录,删除以该数据库为名字的文件夹.cd / ...
- 谁来教我渗透测试——黑客必须掌握的HTML基础(一)
小伙伴们,好几天不见了,这一周菜鸟小白工作很忙,所以没有每天更新学习内容,但是菜鸟小白的学习是没有停下来的,只是没有时间来整理学习笔记了.现在就将菜鸟小白这两天学习的HTML基础和大家分享,其中还会拿 ...
- 智能问答中的NLU意图识别流程梳理
NLU意图识别的流程说明 基于智能问答的业务流程,所谓的NLU意图识别就是针对已知的训练语料(如语料格式为\((x,y)\)格式的元组列表,其中\(x\)为训练语料,\(y\)为期望输出类别或者称为意 ...
- 商业分析-04行为&业务相关数据指标
[访问深度]用户对产品的了解程度 [弹出率] 弹出率是基于访问回话的 而不是基于页面的,上图中1 4 6 是属于弹出
- QT QMdiArea 添加背景或添加背景图片失效问题
说起QMdirArea 这个控件与其他控件真所不同.... 这里记一下 我踩过的坑之一,,,,, QMdiArea 默认的背景 不符合我要求,,当时我就理所当然就想往常一样给它设置颜色 万万没想到.. ...
- Python参数解析工具ArgumentParser
通过命令行运行Python脚本时,可以通过ArgumentParser来高效地接受并解析命令行参数. 流程 新建一个ArgumentParser类对象,然后来添加若干个参数选项,最后通过parse_a ...
- python设计模式之建造者模式
python设计模式之建造者模式 建造者模式的适用范围:想要创建一个由多个部分组成的对象,而且它的构成需要一步接一步的完成.只有当各个部分都完成了,这个对象才完整.建造者模式表现为复杂对象的创建与 ...
- 简单快速导出word文档
最近,我写公司项目word导出功能,应该只有2小时的工作量,却被硬生生的拉长2天,项目上线到业务正常运行也被拉长到2个星期. 为什么如此浪费时间呢? 1)公司的项目比较老,采用硬编码模式,意味着wor ...