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)★ ...
随机推荐
- uiautomator中InteractionController学习笔记(8)
4.1版本号 InteractionController将用户的键盘事件注入到android系统中,与系统进行交互(电视为什么不能设计成可组装,屏幕多大自己决定,想 多大就多大) click(int, ...
- h5-弹出层layer,提示,顶部横条,
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYkAAAI7CAIAAACWVfAJAAAgAElEQVR4nOy9f1ATWb733z3uOA4kIC ...
- 【BZOJ 2038】小Z的袜子
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2038 [算法] 莫队算法 [代码] #include<bits/stdc++. ...
- Oracle 数据库勒索病毒 RushQL 处理办法
处理办法来自Oracle 官方: https://blogs.oracle.com/cnsupport_news/%E5%AF%B9%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A% ...
- maven、spring jdbc 与mysql
做一个简单的有数据库的maven项目,目前还是没有前端,而且没有使用mybatis.之后会向项目中添加. 图片 对于上图的说明,第一个大的表格是未运行测试程序数据表的内容,第二个大的表格是运行测试程序 ...
- Android系统自适应屏幕大小
1.屏幕相关概念1.1分辨率是指屏幕上有横竖各有多少个像素1.2屏幕尺寸指的是手机实际的物理尺寸,比如常用的2.8英寸,3.2英寸,3.5英寸,3.7英寸android将屏幕大小分为四个级别(smal ...
- PHP邮件发送库:Swiftmailer
Swiftmailer需要PHP 7.0或更高版本,(proc_*函数可用.) 安装 composer require "swiftmailer/swiftmailer:^6.0" ...
- 移动端 | Vue.js对比微信小程序基础语法
(1)vue 自定义组件与父组件的通信,props:[abb],可以看成自组建的一个自定义属性 (2)vue 模版语法{{}} 只能是在DOM中插入,<div>{{acc}}</di ...
- jQuery对象与DOM对象的区别
如何判断一个js对象是否一个DOM对象 我们在写js代码时有时需要判断某个对象是不是DOM对象,然后再进行后续的操作,这里我给出一种兼容各大浏览器,同时又算是比较稳妥的一种方法. 要判断一个对象是否D ...
- CoordinatorLayout:android之ScrollingActivity
1.效果图 2.新建SrcollingActivity后生成代码为: <?xml version="1.0" encoding="utf-8"?> ...