World Exhibition

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1383    Accepted Submission(s):
679

Problem Description
Nowadays, many people want to go to Shanghai to visit
the World Exhibition. So there are always a lot of people who are standing along
a straight line waiting for entering. Assume that there are N (2 <= N <=
1,000) people numbered 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.

 
Input
First line: An integer T represents the case of
test.

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.

 
Output
For each line: A single integer. If no line-up is
possible, output -1. If person 1 and N can be arbitrarily far apart, output -2.
Otherwise output the greatest possible distance between person 1 and N.
 
Sample Input
1
4 2 1
1 3 8
2 4 15
2 3 4
 
Sample Output
19
 
题意跟 poj3169  一模一样:分析点我!!!!
#include<stdio.h>
#include<string.h>
#include<queue>
#define MAX 100000
#define INF 0x3f3f3f
using namespace std;
int head[MAX];
int n,m,s,ans;
int dis[MAX],vis[MAX];
int used[MAX];
struct node
{
int u,v,w;
int next;
}edge[MAX];
void add(int u,int v,int w)
{
edge[ans].u=u;
edge[ans].v=v;
edge[ans].w=w;
edge[ans].next=head[u];
head[u]=ans++;
}
void init()
{
ans=0;
memset(head,-1,sizeof(head));
}
void getmap()
{
int i,j;
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
while(s--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(b,a,-c);
}
}
void spfa(int sx)
{
int i,j;
queue<int>q;
memset(vis,0,sizeof(vis));
memset(used,0,sizeof(used));
for(i=1;i<=n;i++)
dis[i]=i==sx?0:INF;
vis[sx]=1;
used[sx]++;
q.push(sx);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(i=head[u];i!=-1;i=edge[i].next)
{
int top=edge[i].v;
if(dis[top]>dis[u]+edge[i].w)
{
dis[top]=dis[u]+edge[i].w;
if(!vis[top])
{
vis[top]=1;
q.push(top);
used[top]++;
if(used[top]>n)
{
printf("-1\n");
return ;
}
}
}
}
}
if(dis[n]==INF)
printf("-2\n");
else
printf("%d\n",dis[n]);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&s);
init();
getmap();
spfa(1);
}
return 0;
}

  

 

hdoj 3952 World Exhibition的更多相关文章

  1. 【HDOJ】3592 World Exhibition

    基础差分约束. /* 3592 */ #include <iostream> #include <algorithm> #include <queue> #incl ...

  2. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  4. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  5. HDOJ 1004 Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  6. hdoj 1385Minimum Transport Cost

    卧槽....最近刷的cf上有最短路,本来想拿这题复习一下.... 题意就是在输出最短路的情况下,经过每个节点会增加税收,另外要字典序输出,注意a到b和b到a的权值不同 然后就是处理字典序的问题,当松弛 ...

  7. HDOJ(2056)&HDOJ(1086)

    Rectangles    HDOJ(2056) http://acm.hdu.edu.cn/showproblem.php?pid=2056 题目描述:给2条线段,分别构成2个矩形,求2个矩形相交面 ...

  8. (01背包变形) Cow Exhibition (poj 2184)

    http://poj.org/problem?id=2184   Description "Fat and docile, big and dumb, they look so stupid ...

  9. POJ2184 Cow Exhibition[DP 状态负值]

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12420   Accepted: 4964 D ...

随机推荐

  1. windows 环境下安装plpython语言环境到postgresql数据库

    1.1       安装plpython 在windows环境 1.1.1      下载http://legacy.python.org/ftp//python/3.2.5/python-3.2.5 ...

  2. PHP实现递归的三种方法

    递归函数是我们常用到的一类函数,最基本的特点是函数自身调用自身,但必须在调用自身前有条件判断,否则无限无限调用下去.实现递归函数可以采取什么方式呢?本文列出了三种基本方式.理解其原来需要一定的基础知识 ...

  3. 截获控制台程序关闭事件(SetConsoleCtrlHandler)

    最近控制台程序中需要捕获控制台关闭事件,在用户关闭的时候进行某些操作,找了一大圈发现了一个方法,通过调用WIN32 API SetConsoleCtrlHandler方法来实现,具体代码如下: usi ...

  4. Groovy 数组操作

    将字符串转为map def str="['汤菜':['1000000028','1000000030'],'肉菜':['1000000032'],'素材':['1000000031']]&q ...

  5. Adding the Test API in The ASP.NET Web API Help Page

    1.通过NuGet引用Web API Test Client 引用玩该DLL会生成如下文件: 这里面就是我们的帮助文档界面 2.在项目属性中进行如下设置,勾选XMl文档文件,并设置路径 3.在项目的A ...

  6. E8.Net 工作流二次开发架构平台

    一.          产品简介 E8.Net工作流开发架构是基于微软.Net技术架构的工作流中间件产品,是国内商业流程管理(BPM)领域在.Net平台上的领先产品,是快速搭建流程管理解决方案的二次开 ...

  7. POJ 1836 Alignment 水DP

    题目: http://poj.org/problem?id=1836 没读懂题,以为身高不能有相同的,没想到排中间的两个身高是可以相同的.. #include <stdio.h> #inc ...

  8. Matlab界面语言切换,自由显示中文或英文语言

    Matlab界面语言切换,自由显示中文或英文语言分享给大家,Matlab是一款商业数学软件,广泛使用于算法的开发.数据发现和数值计算等.不同用户对Matlab显示的语言需求也不一样,一用户习惯使用中文 ...

  9. Android源码中的FLAG为何使用16进制

    1.在阅读源码的时候经常发现有一些标志属性使用一些位操作来判断是否具有该标志,增加标志或者去除标志. 比如View.java中的 /** * This view does not want keyst ...

  10. Uva 10288 Coupons

    Description Coupons in cereal boxes are numbered \(1\) to \(n\), and a set of one of each is require ...