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. Dataset

    1,if(ds == null) 这是判断内存中的数据集是否为空,说明DATASET为空,行和列都不存在!! 2,if(ds.Tables[0].Count == 0) 这应该是在内存中存在一个DAT ...

  2. 错误 1 error C2065: “IDC_LISTBOX”: 未声明的标识符

    错误的可能原因及解决方法如下:1.出错文件中没有包含资源文件ID声明的resource.h文件.在出错文件中加入#include “resource.h”语句. 2.工程附件包含目录的路径下没有res ...

  3. python调用Moxa PCOMM Lite通过串口Ymodem协议发送文件

    本文采用python 2.7编写. 经过长期搜寻,终于找到了Moxa PCOMM Lite.调用PCOMM.DLL可以非常方便的通过串口的Xmodem.Ymodem.Zmodem等协议传输文件,而无需 ...

  4. C语言实现的单链表

    链表是一种线性表,但是并不是顺序存储,而是每个节点里面存储着下一个节点的指针,把存储数据元素的数据串链起来. 单链表的基本实现: typedef int DataType;//定义单链表typedef ...

  5. C#中的委托用法

    当一个函数有返回值的时候用,用Func委托方法. 例如: static int sum(int x) { return x+x; } Func<int> a = sum; 当一个函数没有返 ...

  6. 双系统格式化硬盘后装XP遇到grub rescue的问题

    好奇于深度的Deepin系统,给老电脑装了xp和deepin双系统.无奈07年的机子带Deepin,实在是太卡了.正好想给硬盘重新分区,直接将硬盘格式化,重装了xp.于是,问题来了,开机显示: GRU ...

  7. my9.23(输入输出,写操作)

    #include <iostream> #include <fstream> using namespace std; void write(int data,int len) ...

  8. 解决java.lang.NoClassDefFoundError: org.jdom.Content

    报错:java.lang.NoClassDefFoundError: org.jdom.Content 解决办法: 更新lib目录下的jdom.jar 至最新版本.

  9. ARM内核全解析,从ARM7,ARM9到Cortex-A7,A8,A9,A12,A15到Cortex-A53,A57

    转自: ARM内核全解析,从ARM7,ARM9到Cortex-A7,A8,A9,A12,A15到Cortex-A53,A57 前不久ARM正式宣布推出新款ARMv8架构的Cortex-A50处理器系列 ...

  10. python代码优化技巧

    转自:http://www.douban.com/group/topic/31478102/ 这个资料库还有些不错的好文章: http://www.ibm.com/developerworks/cn/ ...