kuangbin_ShortPathS (POJ 3169)

被cow类题目弄得有些炸裂 想了好久好久写了120多行 依然长跪不起发现计算约束条件的时候还是好多麻烦的地方过不去

然后看了看kuangbin的blog 都是泪啊 差分约束的方式做起来只要70多行啊炒鸡简洁有没有

Ps 给手写queue的实现方式跪一下 真是快的飞起

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <queue>
 5 #include <algorithm>
 6 #define INF 0x3F3F3F3F
 7 using namespace std;
 8
 9 const int MAXN = (int)(1e4 + 10);
10 const int MAXM = (int)(1e5 + 10);
11
12 int n, ml, md, dist[MAXN];
13 int size, head[MAXN], point[MAXM], val[MAXM], nxt[MAXM];
14
15 void add (int from, int to, int value)
16 {
17     val[size] = value;
18     point[size] = to;
19     nxt[size] = head[from];
20     head[from] = size++;
21 }
22
23 int  spfa()
24 {
25     int time[MAXN];//也许以后用cnt做名字比较好
26     bool vis[MAXN];
27     queue<int> q;
28     memset(vis, false, sizeof vis);
29     memset(dist, INF, sizeof dist);
30     memset(time, 0, sizeof time);
31
32     q.push(1);
33     vis[1] = true;
34     dist[1] = 0;
35     while(!q.empty()){
36         int u = q.front();
37         q.pop();
38         vis[u] = false;
39         for(int i = head[u]; ~i; i = nxt[i]){
40             if(dist[point[i]] > dist[u] + val[i]){
41                 dist[point[i]] = dist[u] + val[i];
42                 if(!vis[point[i]]){
43                     vis[point[i]] = true;
44                     q.push(point[i]);
45                     if(++time[point[i]] > n) return -1;
46                 }
47             }
48         }
49     }
50     return dist[n] == INF ? -2 : dist[n];
51 }
52
53 int main()
54 {
55     size = 0;
56     memset(head, -1, sizeof head);
57
58     scanf("%d%d%d", &n, &ml, &md);
59     while(ml--){
60         int from, to, value;
61         scanf("%d%d%d", &from, &to, &value);
62         if(from > to) swap(from, to);
63         add(from, to, value);
64     }
65     while(md--){
66         int from, to, value;
67         scanf("%d%d%d", &from, &to, &value);
68         if(from < to) swap(from, to);
69         add(from, to, -value);
70     }
71     int ans = spfa();
72     printf("%d\n", ans);
73     return 0;
74 }
时间: 2024-10-08 16:11:46

kuangbin_ShortPathS (POJ 3169)的相关文章

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

POJ 3169 Layout (图论-差分约束)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6574   Accepted: 3177 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

POJ 3169 Layout (差分约束系统)

题目地址:POJ 3169 很简单的差分约束..公式很明显.当输入最大值的时候,是a-b<=c,最小值的时候是a-b>=c.然后根据这个式子用最短路求. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctyp

poj 3169 差分约束

3169 差分约束的是满足多组形如xi-yj<=bk{i,j<n k<m}不等式极值问题,可以转化为单源最短路来求. 在最短路中 d[v]<=d[u]+w(u,v) 可以看出跟上面的不等式很像 通常有两种一种是求满足所有不等式的最大值,还有是最小值. 这篇博客可以参考一下 分析: 题目给出了两种不等式  d[u]+dl >=d[v]       d[u]+dd<=d[v]  要求的是最大值,也就是最短路 对于d[u]+dl>=d[v] 建边(u,vdl) 对于d[

poj 3169 Layout(差分约束)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6549   Accepted: 3168 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

poj 3169 Layout (差分约束+Bellman )

题目链接:http://poj.org/problem?id=3169 题意:输入N, ML, MD, N默示有N个牛按1-N排成一排,ML,默示有ML行,每行输入A, B, D默示A牛和B牛最远间隔为D, MD默示有MD行,每行输入A,B,D默示A牛和B来间隔为D,求满足所有前提的1-N的最大间隔. 比较简单的差分约束,这个周周赛的A题 #include <iostream> #include <cstdlib> #include <cstdio> #include

POJ 3169 Layout(差分约束系统)

题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有ML组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 <= w.2.有MD组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 >= w.问如果这n头无法排成队伍,则输出-1,如果牛[1]和牛[n]的距离可以无限远,则输出-2,否则则输出牛[1]和牛[n]之间的最

POJ 3169.Layout 最短路

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11612   Accepted: 5550 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

POJ 3169 Layout(差分约束啊)

题目链接:http://poj.org/problem?id=3169 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