这题建图没什么特别

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 --- 差分约束的更多相关文章

  1. hdu-3592 World Exhibition(差分约束)

    题目链接: World Exhibition Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/ ...

  2. HDU3592(差分约束)

    World Exhibition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. poj 3169&hdu3592(差分约束)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9687   Accepted: 4647 Descriptio ...

  4. 【转】最短路&差分约束题集

    转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...

  5. 差分约束 HDU - 1384 HDU - 3592 HDU - 1531 HDU - 3666

    Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. 转载 - 最短路&差分约束题集

    出处:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548    A strange lift基础最短路(或bfs)★ ...

  7. Candies-POJ3159差分约束

    Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse was the ...

  8. poj3159 差分约束 spfa

    //Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...

  9. ZOJ 2770火烧连营——差分约束

    偶尔做了一下差分约束. 题目大意:给出n个军营,每个军营最多有ci个士兵,且[ai,bi]之间至少有ki个士兵,问最少有多少士兵. ---------------------------------- ...

随机推荐

  1. UNIX环境高级编程--7

    进程环境main函数:    C程序总是从main函数开始执行.main函数原型是:    int main(int argc, char *argv[]);    当内核执行C程序时(使用一个exe ...

  2. 涨知识III - 百度2016校园招聘——移动软件研发工程师

    1.列关于线程调度的叙述中,错误的是(). 正确答案 :BE A调用线程的sleep()方法,可以使比当前线程优先级低的线程获得运行机会 B调用线程的yeild()方法,只会使与当前线程相同优先级的线 ...

  3. [转]linux 下 join命令总结

    转自:http://blog.chinaunix.net/uid-20754793-id-177777.html 有两个文件需要合并,开始写了脚本实现,忽然发现join命令能够完全替代,总结了一下jo ...

  4. 关于debug.keystore文件用法以及错误处理

    在开发过程中需要频繁的为测试的同事签名apk,非常很麻烦,把默认debug.keystore文件替换成发布用(生产环境)的签名文件,不用频繁地签名apk文件了.      如果直接使用生产keysto ...

  5. html——meta标签、link标签

    <meta> 元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词. <meta> 标签位于文档的头部,不包含任何内容.&l ...

  6. UICollectionView框架总结

    一.UIcollectionView介绍 1.1.简介 首先看苹果官方文档 UICollectionView Class Reference 的介绍: The UICollectionView cla ...

  7. UIResponder详解

    UIResponder Class Reference Managing the Responder Chain 1.- (UIResponder *)nextResponder 返回接收者的下一个相 ...

  8. 批量obj格式直接转gltf

    在cesium中的模型需要的是gltf或glb格式的文件,之前的做法是用将模型从3d max中导出dae格式的文件(需要插件),然后用collada2gltf工具将dae格式转成gltf. 最近翻看c ...

  9. On branch master nothing to commit, working tree clean ERROR: Repository not found. fatal: Could not read from remote repository.

    将gitbash部署hexo到github:hexo deploy 报以下错误: Administrator@liu MINGW64 /Hexo $ hexo d INFO Deploying: gi ...

  10. JavaFX桌面应用开发-鼠标事件和键盘事件

    鼠标相关事件的操作初始代码 package application; import javafx.application.Application;import javafx.event.ActionE ...