HDU3592(差分约束)

World Exhibition

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


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
 

 

Author

alpc20
 

 

Source

 
差分约束系统
建图:
问题询问最大值,因此差分约束求最短路。不等式全部转化成 <= 号。
对于 dis[v] - dis[u] <= w  (u < v),从u到v建立一条权值为w的有向边。
对于 dis[v] - dis[u] >= w  (u < v), 将不等式转换为dis[u] - dis[v] <= -w  (u < v),从v到u建立一条权值为-w的有向边。
 
spfa找最短路。
  1 //2017-08-29
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <iostream>
  5 #include <algorithm>
  6 #include <queue>
  7 #include <stack>
  8 
  9 using namespace std;
 10 
 11 const int N = 300100;
 12 const int M = 2500100;
 13 const int INF = 0x3f3f3f3f;
 14 
 15 int head[N], tot;
 16 struct Edge{
 17     int to, next, w;
 18 }edge[M];
 19 
 20 void init(){
 21     tot = 0;
 22     memset(head, -1, sizeof(head));
 23 }
 24 
 25 void add_edge(int u, int v, int w){
 26     edge[tot].w = w;
 27     edge[tot].to = v;
 28     edge[tot].next = head[u];
 29     head[u] = tot++;
 30 }
 31 
 32 int n, m, c;
 33 bool vis[N];
 34 int dis[N], cnt[N];
 35 
 36 bool spfa(int s, int n){
 37     memset(vis, 0, sizeof(vis));
 38     memset(dis, INF, sizeof(dis));
 39     memset(cnt, 0, sizeof(cnt));
 40     vis[s] = 1;
 41     dis[s] = 0;
 42     cnt[s] = 1;
 43     deque<int> dq;
 44     dq.push_back(s);
 45     int sum = 0, len = 1;
 46     while(!dq.empty()){
 47         // LLL 优化
 48         while(dis[dq.front()]*len > sum){
 49             dq.push_back(dq.front());
 50             dq.pop_front();
 51         }
 52         int u = dq.front();
 53         sum -= dis[u];
 54         len--;
 55         dq.pop_front();
 56         vis[u] = 0;
 57         for(int i = head[u]; i != -1; i = edge[i].next){
 58             int v = edge[i].to;
 59             if(dis[v] > dis[u] + edge[i].w){
 60                 dis[v] = dis[u] + edge[i].w;
 61                 if(!vis[v]){
 62                     vis[v] = 1;
 63                     // SLF 优化
 64                     if(!dq.empty() && dis[v] < dis[dq.front()])
 65                       dq.push_front(v);
 66                     else dq.push_back(v);
 67                     sum += dis[v];
 68                     len++;
 69                     if(++cnt[v] > n)return false;
 70                 }
 71             }
 72         }
 73     }
 74     return true;
 75 }
 76 
 77 int main()
 78 {
 79     std::ios::sync_with_stdio(false);
 80     //freopen("input.txt", "r", stdin);
 81     int T, n, x, y;
 82     cin>>T;
 83     while(T--){
 84         init();
 85         cin>>n>>x>>y;
 86         int u, v, w;
 87         while(x--){
 88             cin>>u>>v>>w;
 89             add_edge(u, v, w);
 90         }
 91         while(y--){
 92             cin>>u>>v>>w;
 93             add_edge(v, u, -w);
 94         }
 95         if(spfa(1, n)){
 96             if(dis[n] == INF)cout<<-2<<endl;
 97             else cout<<dis[n]<<endl;
 98         }else cout<<-1<<endl;
 99     }
100 
101     return 0;
102 }

 

转载于:https://www.cnblogs.com/Penn000/p/7449242.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值