poj3169 Layout

Layout

题意: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]之间的最大距离。

代码:

View Code
 1 #include<iostream>
 2 #include<string>
 3 #include<queue>
 4 #include<map>
 5 #include<cmath>
 6 #include<stack>
 7 #include<algorithm>
 8 using namespace std;
 9 // 队列实现,而且有负权回路判断—POJ 3169 Layout
10 
11 const int INF = 0x3F3F3F3F;
12 const int V = 1001;
13 const int E = 20001;
14 int pnt[E], cost[E], nxt[E];
15 int e, head[V], dist[V];
16 bool vis[V];
17 int cnt[V]; // 入队列次数
18 queue<int> que;
19 
20 void init(){
21     e = 0;
22     memset(head, -1, sizeof(head));
23 }
24 
25 inline void addedge(int u, int v, int c){
26     pnt[e] = v; cost[e] = c; nxt[e] = head[u]; head[u] = e++;
27 }
28 
29 int SPFA(int src, int n){
30     int i,u,v;
31 
32     for( i=1; i <= n; ++i ) {
33         dist[i] = INF;cnt[i]=0;vis[i]=1;
34     }
35     
36     while(!que.empty())que.pop();
37     que.push(src);vis[src] =0; 
38     dist[src] = 0;
39     ++cnt[src];
40     
41     while( !que.empty() ){
42         u = que.front();que.pop(); vis[u] =1;
43         for( i=head[u]; i != -1; i=nxt[i] ){
44             v = pnt[i];
45             if(dist[v] > dist[u] + cost[i] ){
46                 dist[v] = dist[u] + cost[i]; 
47                 if(vis[v]){
48                     que.push(v);vis[v]=0;
49                     if((++cnt[v])>n)return -1;//出现负权回路 
50                 }
51             }
52         }
53     }
54     if( dist[n] == INF ) return -2; // src与n不可达
55     return dist[n]; // 返回src到n的最短距离
56 }
57 
58 
59 int main(){
60     int n, ml, md;
61     while( scanf("%d%d%d", &n, &ml, &md) != EOF ){
62         int a, b, c;
63         init();
64         while(ml--){
65             scanf("%d%d%d", &a, &b, &c);
66             if( a > b) swap(a, b);
67             addedge(a, b, c);
68         }
69         while(md--){
70             scanf("%d%d%d", &a, &b, &c);
71             if( a < b ) swap(a, b);
72             addedge(a, b, -c);
73         }
74 
75         printf("%d\n", SPFA(1, n));
76     }
77     return 0;
78 }
原文地址:https://www.cnblogs.com/tiankonguse/p/2614986.html