Layout POJ - 3169 最短路,SPFA算法+负环判断

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 numbered, and since they can be rather pushy, it is possible that two or more cows can line up at exactly the same location (that is, if we think of each cow as being located at some coordinate on a number line, then it is possible for two or more cows to share the same coordinate). 

Some cows 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 ML (1 <= ML <= 10,000) constraints describes which cows like each other and the maximum distance by which they may be separated; a subsequent list of MD constraints (1 <= MD <= 10,000) tells which cows 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 cow 1 and cow N that satisfies the distance constraints.

Input

Line 1: Three space-separated integers: N, ML, and MD. 

Lines 2..ML+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at most D (1 <= D <= 1,000,000) apart. 

Lines ML+2..ML+MD+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at least D (1 <= D <= 1,000,000) apart.

Output

Line 1: A single integer. If no line-up is possible, output -1. If cows 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between cows 1 and N.

Sample Input

4 2 1
1 3 10
2 4 20
2 3 3

Sample Output

27

Hint

Explanation of the sample: 

There are 4 cows. Cows #1 and #3 must be no more than 10 units apart, cows #2 and #4 must be no more than 20 units apart, and cows #2 and #3 dislike each other and must be no fewer than 3 units apart. 

The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.
 
题意s个点,点与点之间有n和m两种情况,n表示点与点之间最大为l,m表示点与点之间最小为l,问1到s之间最大的值时多少。
 
思路:根据题意能得到:n情况是d[1]——d[3]<=10,d[4]——d[2]<=20,m情况:d[3]——d[2]>=3,通过对第三个式子变换得d[2]——d[3]<=-3。因此题意就相当与求带负权的最短路了。
 
代码:
  1 #include <cstdio>
  2 #include <fstream>
  3 #include <algorithm>
  4 #include <cmath>
  5 #include <deque>
  6 #include <vector>
  7 #include <queue>
  8 #include <string>
  9 #include <cstring>
 10 #include <map>
 11 #include <stack>
 12 #include <set>
 13 #include <sstream>
 14 #include <iostream>
 15 #define mod 998244353
 16 #define eps 1e-6
 17 #define ll long long
 18 #define INF 0x3f3f3f3f
 19 using namespace std;
 20 
 21 //结构体存放边的信息
 22 struct node
 23 {
 24     int y,z,next;
 25 };
 26 node no[10005];
 27 //存放边的位置
 28 int head[10005];
 29 //s表示点的数量,n表示ML边的数量,m表示MD边的数量
 30 int s,n,m;
 31 //ans表示边数
 32 int ans;
 33 //表示起点到其他点的最短值
 34 int dis[10005];
 35 //判断当前点的最短值是否找到了
 36 bool vis[10005];
 37 //记录当前点寻找了几次
 38 int cnt[10005];
 39 //求最短路并且判断是否有负环
 40 bool spfa()
 41 {
 42     //初始化
 43     queue<int> qu;
 44     qu.push(1);
 45     memset(dis,INF,sizeof(dis));
 46     memset(vis,0,sizeof(vis));
 47     memset(cnt,0,sizeof(cnt));
 48     //起点的初始化
 49     vis[1]=1;
 50     dis[1]=0;
 51     //队列为空时退出
 52     while(!qu.empty())
 53     {
 54         //出队
 55         int u=qu.front();
 56         qu.pop();
 57         //由于该点的最短值发生了改变,因此与之相连的点的值也要发生变化
 58         vis[u]=0;
 59         //遍历与u相连的边
 60         for(int i=head[u];i!=-1;i=no[i].next)
 61         {
 62             int v=no[i].y;
 63             //更新最小值
 64             if(dis[v]>dis[u]+no[i].z)
 65             {
 66                 dis[v]=dis[u]+no[i].z;
 67                 //如果当前v点标记过则不需要再寻找
 68                 if(!vis[v])
 69                 {
 70                     //标记v点
 71                     vis[v]=1;
 72                     qu.push(v);
 73                     //记录该店寻找的次数
 74                     cnt[v]++;
 75                     //如果次数大于n表示有负环
 76                     if(cnt[v]>n)
 77                     {
 78                         return false;
 79                     }
 80                 }
 81             }
 82         }
 83     }
 84 }
 85 int main()
 86 {
 87     scanf("%d %d %d",&s,&n,&m);
 88     int u,v,l;
 89     ans=1;
 90     //初始化
 91     memset(head,-1,sizeof(head));
 92     //正向记录边
 93     for(int i=1;i<=n;i++)
 94     {
 95         scanf("%d %d %d",&u,&v,&l);
 96         no[ans].y=v;
 97         no[ans].z=l;
 98         no[ans].next=head[u];
 99         head[u]=ans++;
100     }
101     //反向记录边
102     for(int i=1;i<=m;i++)
103     {
104         scanf("%d %d %d",&u,&v,&l);
105         no[ans].y=u;
106         no[ans].z=-l;
107         no[ans].next=head[v];
108         head[v]=ans++;
109     }
110     //如果有负环则无解输出-1,解为无穷表示有很多种情况输出-2,有一种解输出解
111     if(spfa())
112     {
113         if(dis[s]==INF)
114         {
115             printf("-2\n");
116         }
117         else
118         {
119             printf("%d\n",dis[s]);
120         }
121     }
122     else
123     {
124         printf("-1\n");
125     }
126 
127 }

 

转载于:https://www.cnblogs.com/mzchuan/p/11568371.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值