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个士兵,问最少有多少士兵. ---------------------------------- ...
随机推荐
- WPF PasswordBox MVVM 实现
由于PasswordBox.Password属性非依赖属性,所以不能作为绑定的目标,以下是本人的MVVM实现方法. PasswordBox.Password与TextBox.Text同步,TextBo ...
- HTML 表单 存为EXCEL文件时 中文显示乱码
在做宣传品发放系统时,需求要把数据库查询的记录生成表单并转存excel文件. 在转存的EXCEL文件中文显示乱码,表格和其他字符正常,检查后发现是创建EXCEL文件打开模式不对 之前: myfile ...
- Java常用开源jar包
转:http://blog.csdn.net/kevingao/article/details/8125683 activation~与javaMail有关的jar包,使用javaMail时应与mai ...
- Oreacle 语句
SELECT * FROM = and CREATETIME >to_date('2019-01-01' , 'yyyy-mm-dd hh24:mi:ss') and CREATETIME &l ...
- PHP 之递归遍历目录与删除
/** * @Description: 递归查询目录文件 * @Author: Yang * @param $path * @param int $level * @return array */ f ...
- Luogu P2866 [USACO06NOV]糟糕的一天Bad Hair Day
P2866 [USACO06NOV]糟糕的一天Bad Hair Day 题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a ...
- char如何储存3个字节或者4个字节
1.char字符存储的是Unicode编码的代码点,也就是存储的是U+FF00这样的数值,然而我们在调试或者输出到输出流的时候,是JVM或者开发工具按照代码点对应的编码字符输出的. 2. 所以虽然UT ...
- 48.cartinality的基本用法
主要知识点 cartinality的用法 es去重用的是cartinality metric算法,对每个bucket中的指定的field进行去重,然后获取去重后的count,类似于count( ...
- 关于wordclou的一些简单操作
详细讲解一下怎么用python的三方库wordcloud制作一个关于歌曲<Vincent>的歌词,有特别背景的云词效果,如图所示: 首先的先准备好一张背景图,为了云词效果,可以实现修改一下 ...
- eclipse 下使用 git
一.默认仓库文件夹 二.window -->perspective --> customize perspective 三.提交时,要忽略的文件