UVA 558 Wormholes
要问是否存在一个总权重为负数的环,用dfs即可解决。
time:33ms
#include <cstdio>
#include <cstring>
#define N 3000
using namespace std;
int n, m, T, w[N], u[N], v[N], next[N], first[N], pa[N], d[N], tag, i; void read_graph(void)
{
for(int e = ; e < m; e++)
{
scanf("%d%d%d",&u[e], &v[e], &w[e]);
next[e] = first[u[e]];
first[u[e]] = e;
}
}
void dfs(int x, int fa, int dis)
{
pa[x] = fa;
d[x] = dis;
for(int e = first[x]; e != -; e = next[e])
if(tag) return;
else if(pa[v[e]] == -)//这里应该改为pa[v[e]] != x ,要考虑到负权上的点可能事先被访问过,感谢提出
{
if(v[e] == i && d[x] + w[e] < )
{
puts("possible"), tag = ;
return ;
}
if(v[e] == i)
continue;
dfs(v[e], x, d[x] + w[e]);
}
}
int main(void)
{
scanf("%d", &T);
while(T--)
{
memset(first, -, sizeof(first));
tag = ;
scanf("%d%d", &n, &m);
read_graph();
for(i = ; i < n; i++)
{
memset(d, , sizeof(d)), memset(pa, -, sizeof(pa)),
dfs(i, -, );
if(tag)
break;
}
if(!tag)
puts("not possible");
}
return ;
}
或者采用bellman—ford算法判断负权回路,
第n次循环时,若d[y]>d[x] + w[i],也就是能够继续松弛下去说明图中存在负权回路。
time:44ms速度还慢了一点,相比dfs
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define INF 0x0f0f0f0f
#define MAXN 1111
#define MAXM 2222
using namespace std; int d[MAXN];
int u[MAXM], v[MAXM], w[MAXM], next[MAXM], first[MAXM];
int t, n, m, e;
void read_graph(void)
{
scanf("%d%d",&n, &m);
for(e = ; e < m; e++)
{
scanf("%d%d%d",&u[e], &v[e], &w[e]);
next[e] = first[u[e]];
first[u[e]] = e;
}
}
void bellman_ford(void)
{
int i;
for(int k = ; k < n-; k++)
for(i = ; i < e; i++ )
{
int x = u[i], y = v[i];
if(d[x] < INF)
d[y] = min(d[y], d[x] + w[i]);
}
for(i = ; i < e; i++)
{
int x = u[i], y = v[i];
if(d[y] > d[x] + w[i])
{
puts("possible");
break;
}
}
if(i == e)
puts("not possible");
}
int main(void)
{
scanf("%d",&t);
while(t--)
{
memset(d, 0x0f, sizeof(d));
memset(first, -, sizeof(first));
d[] = ;
read_graph();
bellman_ford();
}
return ;
}
然后是spfa算法求解是否存在负权回路,通过判断顶点出队次数大于顶点数n,可知存在负权路,源点的time[0]应该初始化为1,其他的点每松弛一次,次数增加。
time:77ms竟然比bellman—ford,自己写的dfs倒是成了最快的了。
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define MAXN 1111
#define MAXM 2222
using namespace std; struct edgeType{
int v, w;
edgeType(int a, int b):v(a), w(b){}
};
int n,m,t;
int time[MAXN], inq[MAXN], d[MAXN]; vector <edgeType> g[MAXN]; bool spfa(void)
{
queue <int> q;
time[] = ;
q.push();
inq[] = ;
while(!q.empty())
{
int x = q.front();
q.pop();
inq[x] = ;
for(int i = ; i < (int)g[x].size(); i++)
if(d[g[x][i].v] > d[x] + g[x][i].w)
{
d[g[x][i].v] = d[x] + g[x][i].w;
time[g[x][i].v]++;
if(time[g[x][i].v] == n + )
return false;
if(!inq[g[x][i].v])
{
q.push(g[x][i].v);
inq[g[x][i].v] = ;
}
}
}
return true;
}
int main(void)
{
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &m);
int a, b, c;
memset(time, , sizeof(time));
memset(inq, ,sizeof(inq));
memset(d, 0x0f,sizeof(d));
d[] = ;
for(int i = ; i < MAXN; i++)
g[i].clear();
for(int i = ; i < m;i++)
{
scanf("%d%d%d", &a, &b, &c);
g[a].push_back(edgeType(b, c));
}
if(spfa())
puts("not possible");
else puts("possible");
}
return ;
}
UVA 558 Wormholes的更多相关文章
- uva 558 - Wormholes(Bellman Ford判断负环)
题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...
- UVA 558 Wormholes 【SPFA 判负环】
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
- UVA - 558 Wormholes (SPEA算法模板题)
先给出题面:https://vjudge.net/problem/UVA-558 题意描述:给你含n个点以及m条边的图,让你判断在这个图中是否存在负权回路. 首先,我们来介绍什么是SPEA算法 SPF ...
- UVA 558 判定负环,spfa模板题
1.UVA 558 Wormholes 2.总结:第一个spfa,好气的是用next[]数组判定Compilation error,改成nexte[]就过了..难道next还是特殊词吗 题意:科学家, ...
- uva 558 tree(不忍吐槽的题目名)——yhx
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- uva 558 Bellman_Ford
Bellman_Ford算法 求图中是否存在负权值的回路 若图中不存在 则最短路最多经过n-1个结点 若经过超过n-1个节点 则存在负权值的回路 此图永远无法找到最短路 每条边最多 ...
- UVA 558 SPFA 判断负环
这个承认自己没看懂题目,一开始以为题意是形成环路之后走一圈不会产生负值就输出,原来就是判断负环,用SPFA很好用,运用队列,在判断负环的时候,用一个数组专门保存某个点的访问次数,超过了N次即可断定有负 ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- UVa 10012 - How Big Is It? 堆球问题 全排列+坐标模拟 数据
题意:给出几个圆的半径,贴着底下排放在一个长方形里面,求出如何摆放能使长方形底下长度最短. 由于球的个数不会超过8, 所以用全排列一个一个计算底下的长度,然后记录最短就行了. 全排列用next_per ...
随机推荐
- Exchanger, Changing data between concurrent tasks
The Java concurrency API provides a synchronization utility that allows the interchange of data betw ...
- ACM——五位以内的对称素数
http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1026 五位以内的对称素数 时间限制(普 ...
- while循环语句
while(循环条件,返回布尔类型) { 代码执行的操作,或者打印输出. } do whilw循环 do { ...
- [PR & ML 2] [Introduction] Example: Polynomial Curve Fitting
啊啊啊,竟然不支持latex,竟然HTML代码不能包含javascript,代码编辑器也不支持Matlab!!!我要吐槽博客的编辑器...T_T只能贴图凑合看了,代码不是图,但这次为了省脑细胞,写的不 ...
- C++对象模型与内存位对齐的简单分析(GNU GCC&VS2015编译器)
以Fruit和Apple为例进行分析: Fruit和Apple的定义如下: 通过在两种编译环境下的测试(GNU GCC & VS2015),可以发现这两种编译器的对象模型是一样的,如下图所示: ...
- ImageButton如何让图片按比例缩放不被拉伸
了解 在安卓的界面XML中,ImageButton有这样一个属性android:scaleType,他干嘛的? ImageView的Scaletype决定了图片在View上显示时的样子,如进行何种比例 ...
- 6.5 k个已排好序链表合并为一个排序链表
1 建立链表(带哨兵位的)2 建立最小堆方法3 合并已排好序的k个链表 typedef int DataType; //建立链表 class Link { private: struct Node { ...
- WinForm实现最小化窗体时隐藏到系统托盘中
1.首先在工具栏中选择NotifyIcon控件拖入窗体中: 2.设置NotifyIcon控件的相关属性: Icon:在系统托盘中显示的图标: Text:当鼠标移动到系统托盘图标上时显示的文本: Con ...
- 还原数据库备份文件时,关于“System.Data.SqlClient.SqlError:媒体集有2个媒体簇,但只提供了1个。必须提供所有成员”的处理方式
好久没写博客了,最近在做毕设的权限管理模块,今天在还原数据库文件时,遇到了“System.Data.SqlClient.SqlError:媒体集有2个媒体簇,但只提供了1个.必须提供所有成员”这个错误 ...
- AVAudioRecorder 录制音频
AVFoundation 中使用AVAudioRecorder 类添加音频录制功能是非常简单的, AVAudioRecorder构建与Audio Queue Services之上是一个功能强大且代码简 ...