HDU 3592 World Exhibition (差分约束,spfa,水)

题意:

  有n个人在排队,按照前后顺序编号为1~n,现在对其中某两人的距离进行约束,有上限和下限,表示dis[a,b]<=c或者dis[a,b]>=c,问第1个人与第n个人的距离最多可能为多少?(若INF则输出-2,若冲突则输出-1,否则输出距离)

思路:

  建图时都将约束转成a-b<=c的标准形式,然后建一条b->a的边,权为c。然后求最短路,注意最短路跑出来的结果却是最远的合法距离,而不是最短距离。本题无需添加辅助边,只要到达不了n,则距离为INF,输出-2,若有负环,那肯定是冲突了,为-1。

 1 //#include <bits/stdc++.h>
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <set>
 7 #include <deque>
 8 #include <map>
 9 #include <algorithm>
10 #include <vector>
11 #include <iostream>
12 #define pii pair<int,int>
13 #define back que[rear-1]
14 #define INF 0x3f3f3f3f
15 #define LL long long
16 #define ULL unsigned long long
17 using namespace std;
18 const double PI  = acos(-1.0);
19 const int N=1010;
20 struct node
21 {
22     int from,to,dis,next;
23     node(){};
24     node(int from,int to,int dis,int next):from(from),to(to),dis(dis),next(next){};
25 }edge[N*N];
26
27 int edge_cnt, head[N];
28 int inq[N], cnt[N], dist[N], n;
29
30 void add_node(int from,int to,int dis)
31 {
32     edge[edge_cnt]=node(from,to,dis,head[from]);
33     head[from]=edge_cnt++;
34 }
35
36 int spfa(int st,int ed)
37 {
38     memset(cnt,0,sizeof(cnt));//入队次数
39     memset(inq,0,sizeof(inq));//是否在队中
40     memset(dist,0x3f,sizeof(dist));//距离
41     deque<int> que(1,st);
42     inq[st]=1;
43     dist[st]=0;
44     while(!que.empty())
45     {
46         int t=que.front();que.pop_front();
47         inq[t]=0;node e;
48         for(int i=head[t]; i!=-1; i=e.next)
49         {
50             e=edge[i];
51             if( dist[e.to]>dist[t]+e.dis )
52             {
53                 dist[e.to]=dist[t]+e.dis;
54                 if(!inq[e.to])  //没有在队列中
55                 {
56                     if(++cnt[e.to]>n)   //入队次数过多
57                         return -1;
58                     inq[e.to]=1;//下面是优化,可删
59                     if(!que.empty() && dist[e.to]<dist[que.front()])
60                         que.push_front(e.to);
61                     else    que.push_back(e.to);
62                 }
63             }
64
65         }
66     }
67     return dist[ed]==INF?-2:dist[ed];
68 }
69
70 void init()
71 {
72     edge_cnt=0;
73     //for(int i=0; i<=n; i++)   head[i]=-1;
74     memset(head,-1,sizeof(head));
75 }
76
77 int main()
78 {
79     freopen("input.txt", "r", stdin);
80     int x, y, a, b, c, t;cin>>t;
81     while(t--)
82     {
83         init();
84         scanf("%d%d%d",&n,&x,&y);
85         for(int i=1; i<=x; i++) //最多
86         {
87             scanf("%d%d%d",&a,&b,&c);
88             add_node(a,b,c);
89         }
90         for(int i=1; i<=y; i++) //最小
91         {
92             scanf("%d%d%d",&a,&b,&c);
93             add_node(b,a,-c);
94         }
95         printf("%d\n", spfa(1,n));
96     }
97     return 0;
98 }

AC代码

时间: 2024-10-15 07:20:45

HDU 3592 World Exhibition (差分约束,spfa,水)的相关文章

HDU 1384 Intervals【差分约束-SPFA】

类型:给出一些形如a−b<=k的不等式(或a−b>=k或a−b<k或a−b>k等),问是否有解[是否有负环]或求差的极值[最短/长路径].例子:b−a<=k1,c−b<=k2,c−a<=k3.将a,b,c转换为节点:k1,k2,k3转换为边权:减数指向被减数,形成一个有向图: 由题可得(b−a) + (c−b) <= k1+k2,c−a<=k1+k2.比较k1+k2与k3,其中较小者就是c−a的最大值.由此我们可以得知求差的最大值,即上限被约束,此时我

HDOJ 3592 World Exhibition 差分约束

World Exhibition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1373    Accepted Submission(s): 673 Problem Description Nowadays, many people want to go to Shanghai to visit the World Exhibiti

hdu3592 World Exhibition --- 差分约束

这题建图没什么特别 x个条件:Sb-Sa<=c y个条件:Sa-Sb<=-c 题目问的是,1和n之间的关系. 有负环的话,整个就不可能成立,输出-1 如果图是连通的(1到n是连通的),就输出d[n] 不连通就是题目中说-2的情况. 原来我们建图一般添加一个附加结点,或者开始就把所有点入队,就是考虑到不连通的问题,所以添加一个没有意义的条件. #include <iostream> #include <cstring> #include <string> #i

POJ 3169 Layout (差分约束+SPFA)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6832   Accepted: 3292 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a

ZOJ 2770 Burn the Linked Camp 差分约束+SPFA

第一道正儿八经的差分约束题 有排成一列的n个点,首先告诉你每个点的值最多是多少(最少显然要大于0),然后告诉你m段i,j,k,表示第i个点到第j个点的值的和至少有k,问你总和至少为多少. 要注意的是,告诉你的所有关系式都不要忘记建边,一开始漏了大于0的条件调半天o(╯□╰)o 不等式的形式是a-b<=c这样的= = 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <

【bzoj2330】: [SCOI2011]糖果 图论-差分约束-SPFA

[bzoj2330]: [SCOI2011]糖果 恩..就是裸的差分约束.. x=1 -> (A,B,0) (B,A,0) x=2 -> (A,B,1)  [这个情况加个A==B无解的要特判] x=3 -> (B,A,0)  [恩这个是不少于一开始zz建反了] x=4 -> (B,A,1) x=5 -> (A,B,0) 然后源点到所有点建1的边[恩据说有条链所以要反着连]跑最长路就好了 1 /* http://www.cnblogs.com/karl07/ */ 2 #inc

poj3159 差分约束 spfa

1 //Accepted 2692 KB 1282 ms 2 //差分约束 -->最短路 3 //TLE到死,加了输入挂,手写queue 4 #include <cstdio> 5 #include <cstring> 6 #include <iostream> 7 #include <queue> 8 #include <cmath> 9 #include <algorithm> 10 using namespace std;

poj3159——Candies(差分约束+SPFA堆栈)

Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse's class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and ofte

(简单) POJ 3169 Layout,差分约束+SPFA。

Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbe

poj3169——Layout(差分约束+SPFA判断负环)

Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbe