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)★ ...
随机推荐
- UVA 11346 - Probability 数学积分
Consider rectangular coordinate system and point L(X, Y ) which is randomly chosen among all pointsi ...
- centos6配置固定ip地址(选择桥接模式)
1.配置ifcfg-eth0 vim /etc/sysconfig/network-scripts/ifcfg-eth0 首先把BOOTPROTO="dhcp"改成BOOTPROT ...
- 将字符串序列化Object格式
using Newtonsoft.Json; 首先引用 Newtonsoft.Json; 定义一个字符串 string str = "[{'ID':8.0,'PAGEID':201.0,' ...
- 36.QT地图
widget.h #ifndef MAPWIDGET_H #define MAPWIDGET_H #include <QGraphicsView> #include <QLabel& ...
- Google浏览器vim命令
使用鼠标久了,手腕.肩膀依旧疼痛.偶尔逛知乎,看到有人推荐chrome浏览器的vimium插件(火狐浏览器是vimperator),安装了使用了几天,真不愧是浏览器神器,好用到想哭,而且非常容易上手. ...
- pyspider architecture--官方文档
原文地址:http://docs.pyspider.org/en/latest/Architecture/ Architecture This document describes the reaso ...
- mysql学习 1
1.数据库(Database)是按照数据结构来组织.存储和管理数据的仓库 2.RDBMS即关系数据库管理系统(Relational Database Management System)的特点: 1) ...
- firstChild与firstElementChild
相同点: 都是获取父元素下的第一个节点对象 不同点: firstChild: IE6.7.8 第一个元素节点; 非IE6.7.8:返回第一个元素节点或文本节点 firstElementChild: I ...
- SpringMVC视频
视频内容: 1.下载spring mvc以及spring mvc示例演示http://pan.baidu.com/s/1kTHRfDH 2.配置完善&初步探究控制器拦截http://pan.b ...
- iOS原生数据存储策略
一 @interface NSCache : NSObject Description A mutable collection you use to temporarily store transi ...