hdu3592 World Exhibition --- 差分约束
这题建图没什么特别
x个条件:Sb-Sa<=c
y个条件:Sa-Sb<=-c
题目问的是。1和n之间的关系。
有负环的话,整个就不可能成立,输出-1
假设图是连通的(1到n是连通的),就输出d[n]
不连通就是题目中说-2的情况。
原来我们建图一般加入一个附加结点,或者開始就把全部点入队,就是考虑到不连通的问题,所以加入一个没有意义的条件。
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define inf 0x3f3f3f3f
#define eps 1e-6
#define ll __int64
using namespace std;
#define N 1010 struct node
{
int v,w,next;
}e[30020];
int d[N],inq[N],outq[N],n,head[N],h; void addedge(int a,int b,int c)
{
e[h].v=b;
e[h].w=c;
e[h].next=head[a];
head[a]=h++;
} int spfa(int s)
{
memset(d,0x3f,sizeof d);
memset(inq,0,sizeof inq);
memset(outq,0,sizeof outq);
d[s]=0;inq[s]=1;
queue<int> q;
q.push(s);
int i,x;
while(!q.empty())
{
x=q.front();
q.pop();
inq[x]=0;
outq[x]++;
if(outq[x]>n) return 0;
for(i=head[x];i!=-1;i=e[i].next)
{
if(d[e[i].v]>d[x]+e[i].w)
{
d[e[i].v]=d[x]+e[i].w;
if(!inq[e[i].v])
{
inq[e[i].v]=1;
q.push(e[i].v);
}
}
}
}
return 1;
} void init()
{
memset(head,-1,sizeof head);
h=0;
} int main()
{
int T,a,b,c,x,y;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d%d%d",&n,&x,&y);
while(x--)
{
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
}
while(y--)
{
scanf("%d%d%d",&a,&b,&c);
addedge(b,a,-c);
}
if(!spfa(1))
printf("-1\n");
else if(d[n]!=inf)
printf("%d\n",d[n]);
else printf("-2\n");
}
return 0;
}
hdu3592 World Exhibition --- 差分约束的更多相关文章
- hdu-3592 World Exhibition(差分约束)
题目链接: World Exhibition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- HDU3592(差分约束)
World Exhibition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- poj 3169&hdu3592(差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9687 Accepted: 4647 Descriptio ...
- 【转】最短路&差分约束题集
转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...
- 差分约束 HDU - 1384 HDU - 3592 HDU - 1531 HDU - 3666
Intervals Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 转载 - 最短路&差分约束题集
出处:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★ ...
- Candies-POJ3159差分约束
Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse was the ...
- poj3159 差分约束 spfa
//Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...
- ZOJ 2770火烧连营——差分约束
偶尔做了一下差分约束. 题目大意:给出n个军营,每个军营最多有ci个士兵,且[ai,bi]之间至少有ki个士兵,问最少有多少士兵. ---------------------------------- ...
随机推荐
- Unity学习-软件的基本操作(二)
基本操作 1:Scene中 以小手显示,平移画布,与鼠标中键一样 2:平移 游戏对象,组件的 Position属性 也可设置 3:旋转 游戏对象,组件的 Rotation属性 也可设置 4:缩放 游戏 ...
- mha0.56版本安装使用排错
1.master_check_ssh --conf=/etc/app1.conf 这个检查就报错的我觉得百分之九十都是ssh之间连接问题.务必要保证各节点之间都可以免秘钥访问! 2.mas ...
- 源码安装apache脚本
#!/bin/bash#By:zhaocheng#Date:2019-01-18 [ -d /media/cdrom ] || mkdir /media/cdrom[ -d /media/cdrom/ ...
- LINUX -- pthread_detach()与pthread_join()
pthread_detach()即主线程与子线程分离,子线程结束后,资源自动回收. int pthread_join(pthread_t tid, void **thread_return); {su ...
- (JS,JAVA,MySql)去除小数后多余的0
分别通过JS,JAVA和MySql实现去除小数后多余的0 1. JS方法 /** *去除小数点后多余的0 */ function cutZero(old) { //拷贝一份 返回去掉零的新串 old ...
- Python 字符串和数字
Python 变量类型 变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的数据 ...
- copy contents of file with variable number in Matlab
input : transient.case output: transient_1.case, transient_2.case, transient_3.case ... ************ ...
- saving snaps iteratively with for loop in Paraview
Goal: read data 1 and slice, then save pressure, velocity contours, close data, then do loop for the ...
- Spring整合Junit框架进行单元测试Demo
一.开发环境 eclipse版本:4.6.1 maven版本:3.3.3 junit版本:4.12 spring版本:4.1.5.RELEASE JDK版本:1.8.0_111 二.项目结构 图 三. ...
- libcloud代码研究(三)——bugs
Bug 1:对不可迭代类进行迭代(libcloud.storage.driver.cloudfile line. 141-142) 使用libcloud连接自搭建swift服务,自己在服务器 ...