hdu 3592 差分约束 first bolld
2017-07-12 22:50
357 查看
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.
InputFirst 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.
OutputFor 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
Sample Output
19
题意:n,x,y n个点 x组喜欢,y组不喜欢。
如果不喜欢则a-b<=-c,如果喜欢b-a<=c;
差分约束如果A-B<=2 B到2的距离为2,建边。注意题目隐藏条件如(> 或者< 取<=另 c-1即可
//china no.1
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
using namespace std;
#define pi acos(-1)
#define endl '\n'
#define rand() srand(time(0));
#define me(x) memset(x,-1,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0);
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,-1,-1,1,1};
const int dy[]={0,1,0,-1,1,-1,1,-1};
const int maxn=1e5+5;
const int maxx=1e5+100;
const double EPS=1e-7;
const int MOD=10000007;
#define mod(x) ((x)%MOD);
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;
long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while
int n,x,y,inq[maxn],t,d[maxn],out[maxn];
vector<pair<int,int> >E[maxn];
inline int Scan()
{
int res=0,ch,flag=0;
if((ch=getchar())=='-')flag=1;
else if(ch>='0' && ch<='9')res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-'0';
return flag ? -res : res;
}
void init()
{
for(int i=0;i<maxn;i++)
{
E[i].clear();
inq[i]=0;
d[i]=INF;
}
t=0;
me(out);
}
int SPFA()
{
int s=1;
queue<int >Q;
Q.push(s),d[s]=0,inq[s]=1;
W(!Q.empty())
{
int now=Q.front();
Q.pop();
++out[now];
if(out[now]>n) return 0;//判负环
inq[now]=0;
for(int i=0;i<E[now].size();i++)
{
int v=E[now][i].first;
if(d[v]>d[now]+E[now][i].second)
{
d[v]=d[now]+E[now][i].second;
if(inq[v]==1) continue;
inq[v]=1;
Q.push(v);
}
}
}
if(d
!=INF)
cout<<d
<<endl;
else puts("-2");
return 1;
}
int main()
{
int T;
cin>>T;
W(T--)
{
init();
cin>>n>>x>>y;
int a,b,c;
W(x--)
{
a=Scan();b=Scan();c=Scan();
E[a].push_back(make_pair(b,c));
}
W(y--)
{
a=Scan();b=Scan();c=Scan();
E[b].push_back(make_pair(a,-c));
}
int t=SPFA();
if(t==0)
puts("-1");
}
}
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.
InputFirst 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.
OutputFor 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
题意:n,x,y n个点 x组喜欢,y组不喜欢。
如果不喜欢则a-b<=-c,如果喜欢b-a<=c;
差分约束如果A-B<=2 B到2的距离为2,建边。注意题目隐藏条件如(> 或者< 取<=另 c-1即可
//china no.1
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
using namespace std;
#define pi acos(-1)
#define endl '\n'
#define rand() srand(time(0));
#define me(x) memset(x,-1,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0);
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,-1,-1,1,1};
const int dy[]={0,1,0,-1,1,-1,1,-1};
const int maxn=1e5+5;
const int maxx=1e5+100;
const double EPS=1e-7;
const int MOD=10000007;
#define mod(x) ((x)%MOD);
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;
long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while
int n,x,y,inq[maxn],t,d[maxn],out[maxn];
vector<pair<int,int> >E[maxn];
inline int Scan()
{
int res=0,ch,flag=0;
if((ch=getchar())=='-')flag=1;
else if(ch>='0' && ch<='9')res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-'0';
return flag ? -res : res;
}
void init()
{
for(int i=0;i<maxn;i++)
{
E[i].clear();
inq[i]=0;
d[i]=INF;
}
t=0;
me(out);
}
int SPFA()
{
int s=1;
queue<int >Q;
Q.push(s),d[s]=0,inq[s]=1;
W(!Q.empty())
{
int now=Q.front();
Q.pop();
++out[now];
if(out[now]>n) return 0;//判负环
inq[now]=0;
for(int i=0;i<E[now].size();i++)
{
int v=E[now][i].first;
if(d[v]>d[now]+E[now][i].second)
{
d[v]=d[now]+E[now][i].second;
if(inq[v]==1) continue;
inq[v]=1;
Q.push(v);
}
}
}
if(d
!=INF)
cout<<d
<<endl;
else puts("-2");
return 1;
}
int main()
{
int T;
cin>>T;
W(T--)
{
init();
cin>>n>>x>>y;
int a,b,c;
W(x--)
{
a=Scan();b=Scan();c=Scan();
E[a].push_back(make_pair(b,c));
}
W(y--)
{
a=Scan();b=Scan();c=Scan();
E[b].push_back(make_pair(a,-c));
}
int t=SPFA();
if(t==0)
puts("-1");
}
}
相关文章推荐
- hdu 3592 差分约束 first bolld
- hdu 3592 差分约束 first bolld
- hdu 3592 差分约束 first bolld
- hdu 3592 差分约束 first bolld
- hdu 3592 差分约束 first bolld
- hdu 3592 差分约束 first bolld
- hdu 3592 差分约束 first bolld
- hdu 3592 差分约束 first bolld
- hdu 3592 差分约束 first bolld
- hdu 3592 差分约束 first bolld
- hdu 3592 差分约束 first bolld
- hdu 3592 差分约束 first bolld
- hdu-3592 World Exhibition(差分约束)
- HDU 3592 World Exhibition(差分约束)
- HDU ~ 3592 ~ World Exhibition (SPFA + 差分约束)
- HDU 3592 World Exhibition 差分约束基础题
- hdu 3592 World Exhibition(差分约束,最短路)
- |hdu 3592|差分约束|World Exhibition
- hdu 3592(差分约束)
- HDU 3592 World Exhibition (差分约束,spfa,水)