2014
11-27

# World Exhibition

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.

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.

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.

1
4 2 1
1 3 8
2 4 15
2 3 4

19

http://poj.org/problem?id=3169

http://acm.hdu.edu.cn/showproblem.php?pid=3592

A-B <=X

C-D>=Y也就是  D-C<=Y

PS:差分约束还剩两题，等下去刷完写个总结~

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN=1000+10;
const int MAXM=200000+1000;
const int INF=100000000;
struct edge
{
int to;
int val;
int next;
}e[MAXM];

{
e[len].to=to;
e[len].val=val;
}

int spfa()
{
int start=1;
bool vis[MAXN]={0};
int cnt[MAXN]={0};
deque<int> q;
q.push_back(start);
vis[start]=1;
cnt[start]=1;
dis[start]=0;
while(!q.empty())
{
int cur=q.front();
q.pop_front();
vis[cur]=false;
{
int id=e[i].to;
if(dis[id] > dis[cur] + e[i].val)
{
dis[id]=dis[cur] + e[i].val;
if(!vis[id])
{
if(++cnt[id] > n)
return -1;
vis[id]=true;
if(!q.empty() && dis[id] > dis[q.front()])
q.push_back(id);
else
q.push_front(id);
}
}
}
}

if(dis[n]==INF)
return -2;

return dis[n];
}

int main()
{
while(~scanf("%d%d%d",&n,&ml,&md))
{
len=0;
for(int i=1;i<=n;i++)
{
dis[i]=INF;
}

for(int i=0;i<ml;i++)
{
int from,to,val;
scanf("%d%d%d",&from,&to,&val);
}

for(int i=0;i<md;i++)
{
int from,to,val;
scanf("%d%d%d",&from,&to,&val);
}

printf("%d\n",spfa());

}
return 0;
}

1. 有一点问题。。后面动态规划的程序中
int dp[n+1][W+1];
会报错 提示表达式必须含有常量值。该怎么修改呢。。

2. 第23行：
hash = -1是否应该改成hash[s ] = -1

因为是要把从字符串s的start位到当前位在hash中重置

修改提交后能accept，但是不修改居然也能accept