hdoj--3592--World Exhibition(差分约束)
World Exhibition
1..N who are standing in the same order as they are numbered. It is possible that two or more person line up at exactly the same location in the condition that those visit it in a group.
There is something interesting. Some like each other and want to be within a certain distance of each other in line. Some really dislike each other and want to be separated by at least a certain distance. A list of X (1 <= X <= 10,000) constraints describes
which person like each other and the maximum distance by which they may be separated; a subsequent list of Y constraints (1 <= Y <= 10,000) tells which person dislike each other and the minimum distance by which they must be separated.
Your job is to compute, if possible, the maximum possible distance between person 1 and person N that satisfies the distance constraints.
The next line: Three space-separated integers: N, X, and Y.
The next X lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= N. Person A and B must be at most C (1 <= C <= 1,000,000) apart.
The next Y lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= C. Person A and B must be at least C (1 <= C <= 1,000,000) apart.
1
4 2 1
1 3 8
2 4 15
2 3 4
19
后y条信息说明a--b之间最少相差c,求1--n之间的最大距离,若距
离任意输出-1,如果不存在输出-2,否则就输出最大距离
建图条件:(以1为源点)
1.dis[b]-dis[a]<=c
2.dis[b]-dis[a]>=c----dis[a]-dis[b]<=-c
3.dis[i+1]-dis[i]>=0----dis[i]-dis[i+1]<=0
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 10010
#define MAXM 500000
#define INF 0x3f3f3f
int head[MAXN],vis[MAXN],dis[MAXN],used[MAXN];
int n,x,y,cnt;
struct node
{
int u,v,val;
int next;
}edge[MAXM];
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
}
void add(int u,int v,int val)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].val=val;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void getmap()
{
for(int i=1;i<n;i++)
add(i+1,i,0);
int a,b,c;
while(x--)
{
cin>>a>>b>>c;
add(a,b,c);
}
while(y--)
{
cin>>a>>b>>c;
add(b,a,-c);
}
}
void SPFA()
{
memset(vis,0,sizeof(vis));
memset(dis,INF,sizeof(dis));
dis[1]=0;
vis[1]=1;
used[1]++;
queue<int>q;
q.push(1);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(dis[E.v]>dis[u]+E.val)
{
dis[E.v]=dis[u]+E.val;
if(!vis[E.v])
{
vis[E.v]=1;
used[E.v]++;
if(used[E.v]>n)
{
cout<<-1<<endl;
return ;
}
q.push(E.v);
}
}
}
}
if(dis[n]>=INF)
cout<<-2<<endl;
else
cout<<dis[n]<<endl;
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>x>>y;
init();
getmap();
SPFA();
}
return 0;
}
hdoj--3592--World Exhibition(差分约束)的更多相关文章
- HDOJ 1534 Schedule Problem 差分约束
差分约数: 求满足不等式条件的尽量小的值---->求最长路---->a-b>=c----> b->a (c) Schedule Problem Time Limit: 2 ...
- hdu-3592 World Exhibition(差分约束)
题目链接: World Exhibition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- hdu3592 World Exhibition --- 差分约束
这题建图没什么特别 x个条件:Sb-Sa<=c y个条件:Sa-Sb<=-c 题目问的是.1和n之间的关系. 有负环的话,整个就不可能成立,输出-1 假设图是连通的(1到n是连通的),就输 ...
- 【HDOJ】3592 World Exhibition
基础差分约束. /* 3592 */ #include <iostream> #include <algorithm> #include <queue> #incl ...
- 差分约束 HDU - 1384 HDU - 3592 HDU - 1531 HDU - 3666
Intervals Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- POJ 3169 Layout (HDU 3592) 差分约束
http://poj.org/problem?id=3169 http://acm.hdu.edu.cn/showproblem.php?pid=3592 题目大意: 一些母牛按序号排成一条直线.有两 ...
- HDOJ 1384 差分约束
结题报告合集请戳:http://972169909-qq-com.iteye.com/blog/1185527 /*题意:求符合题意的最小集合的元素个数 题目要求的是求的最短路, 则对于 不等式 f( ...
- 图论--差分约束--HDU\HDOJ 4109 Instrction Arrangement
Problem Description Ali has taken the Computer Organization and Architecture course this term. He le ...
- 【转】最短路&差分约束题集
转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...
- 转载 - 最短路&差分约束题集
出处:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★ ...
随机推荐
- Http multipart/form-data多参数Post方式上传数据
最近,工作中遇到需要使用java实现http发送get.post请求,简单的之前经常用到,但是这次遇到了上传文件的情况,之前也没深入了解过上传文件的实现,这次才知道通过post接口也可以,是否还有其他 ...
- 【POJ 2352】 Stars
[题目链接] http://poj.org/problem?id=2352 [算法] 树状数组 注意x坐标为0的情况 [代码] #include <algorithm> #include ...
- 理解HashMap底层原理,一个简单的HashMap例子
package com.jl.testmap; /** * 自定义一个HashMap * @author JiangLai * */ public class MyHashMap<K,V> ...
- ffmpeg常用指令
在osx系统下通过ffmpeg查看设备 ffmpeg -f avfoundation -list_devices true -i "" -f 指定的是输入输出格式, -i指定输入的 ...
- 通过adb 设置、删除、获取 系统配置值。
通过adb 设置.删除.获取 系统配置值. Key定义在:frameworks\base\core\java\android\provider\Settings.java adb shell sett ...
- Pyhton学习——Day47
# 转载:http://www.cnblogs.com/yuanchenqi/articles/6357507.html# 外键:一种约束条件,与主键对应# 主表:被绑定的表:字表# 外键约束:# - ...
- Python_study_day_1_while_if
1.什么是计算机 cpu:大脑 3GHz 内存:缓冲硬盘和cpu,提高计算机运算速度 硬盘:存储数据 2.编程语言的简单分类 编译型,解释型,混合型 3.python是什么编程语言 解释类语言 //. ...
- TensorFlow+实战Google深度学习框架学习笔记(6)----神经网络经典损失函数
1.分类问题(交叉熵): (1)模型: (2)代码1: 其中,tf.clip_by_value是将一个张量的数值限制在一个范围之内,若小于1e-10则赋值为1e-10,若大于1.0则赋值为1,这样避免 ...
- Mysql干货收集
mysql优化:https://www.cnblogs.com/duanxz/tag/mysql/default.html?page=1
- struts2实现图片验证码
生成图片验证码的主要工具类方法为: package com.yeting.fc.util; import java.awt.Color; import java.awt.Font; import ja ...