设为首页 收藏本站
查看: 914|回复: 0

[经验分享] poj 2391(二分+floyd+sap)

[复制链接]

尚未签到

发表于 2015-9-17 12:57:55 | 显示全部楼层 |阅读模式
  不难的一道网络流,因为为递增性,所以可以用二分,用二分还是相当的快,然后就是基本的建图,和sap了。
  



Ombrophobic Bovines


Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10819 Accepted: 2419

Description


FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes them shake in their hooves. They have decided to put a rain siren on the farm to let them know when rain is approaching. They intend to create a rain evacuation plan so that all the cows can get to shelter before the rain begins. Weather forecasting is not always correct, though. In order to minimize false alarms, they want to sound the siren as late as possible while still giving enough time for all the cows to get to some shelter.
The farm has F (1 <= F <= 200) fields on which the cows graze. A set of P (1 <= P <= 1500) paths connects them. The paths are wide, so that any number of cows can traverse a path in either direction.
Some of the farm's fields have rain shelters under which the cows can shield themselves. These shelters are of limited size, so a single shelter might not be able to hold all the cows. Fields are small compared to the paths and require no time for cows to traverse.
Compute the minimum amount of time before rain starts that the siren must be sounded so that every cow can get to some shelter.
Input


* Line 1: Two space-separated integers: F and P
* Lines 2..F+1: Two space-separated integers that describe a field. The first integer (range: 0..1000) is the number of cows in that field. The second integer (range: 0..1000) is the number of cows the shelter in that field can hold. Line i+1 describes field i.
* Lines F+2..F+P+1: Three space-separated integers that describe a path. The first and second integers (both range 1..F) tell the fields connected by the path. The third integer (range: 1..1,000,000,000) is how long any cow takes to traverse it.
Output


* Line 1: The minimum amount of time required for all cows to get under a shelter, presuming they plan their routes optimally. If it not possible for the all the cows to get under a shelter, output "-1".
Sample Input

3 4
7 2
0 4
2 6
1 2 40
3 2 70
2 3 90
1 3 120
Sample Output

110
Hint


OUTPUT DETAILS:
In 110 time units, two cows from field 1 can get under the shelter in that field, four cows from field 1 can get under the shelter in field 2, and one cow can get to field 3 and join the cows from that field under the shelter in field 3. Although there are other plans that will get all the cows under a shelter, none will do it in fewer than 110 time units.
Source


USACO 2005 March Gold



  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <string>
  4 #include <algorithm>
  5 #include <iostream>
  6 using namespace std;
  7 #define INF 0x3ffffff
  8 #define MAX 1000000000000000LL
  9 #define N 440
10 #define M 10000000
11 struct node
12 {
13     int to,w,next;
14 }edge[M];
15
16 struct node1
17 {
18     int u,v;
19     __int64 w;
20 }g[1505];
21
22 int n,m;
23 int cnt,pre[N];
24 int s,t;
25 int savex[N],savey[N];
26 __int64 map[N][N];
27 int ans;
28 int lv[N],gap[N];
29 int nn;
30
31 void add_edge(int u,int v,__int64 w)
32 {
33     edge[cnt].to=v;
34     edge[cnt].w=w;
35     edge[cnt].next=pre;
36     pre=cnt++;
37 }
38
39 int sdfs(int k,int w)
40 {
41     if(k==t)
42         return w;
43     int f=0;
44     int mi=nn-1;
45     for(int p=pre[k];p!=-1;p=edge[p].next)
46     {
47         int v=edge[p].to;
48         if(edge[p].w!=0)
49         {
50             if(lv[k]==lv[v]+1)
51             {
52                 int tmp=sdfs(v,min(w-f,edge[p].w));
53                 f+=tmp;
54                 edge[p].w-=tmp;
55                 edge[p^1].w+=tmp;
56                 if(f==w||lv==nn) return f;
57             }
58             if(lv[v]<mi) mi=lv[v];
59         }
60     }
61     if(f==0)
62     {
63         gap[lv[k]]--;
64         if( gap[lv[k]]==0 )
65         {
66             lv=nn;
67             return f;
68         }
69         lv[k]=mi+1;
70         gap[lv[k]]++;
71     }
72     return f;
73 }
74
75 int sap()
76 {
77     nn=2*n+2;
78     int sum=0;
79     memset(lv,0,sizeof(lv));
80     memset(gap,0,sizeof(gap));
81     gap[0]=nn;
82     while(lv<nn)
83     {
84         sum+=sdfs(s,INF);
85     }
86     return sum;
87 }
88
89 int check(__int64 mid)
90 {
91     cnt=0;
92     memset(pre,-1,sizeof(pre));
93     for(int i=1;i<=n;i++)
94     {   
95         add_edge(s,i,savex);
96         add_edge(i,s,0);
97         
98         add_edge(n+i,t,savey);
99         add_edge(t,n+i,0);
100         
101         add_edge(i,n+i,INF);
102         add_edge(n+i,i,0);
103     }
104     for(int i=1;i<=n;i++)
105         for(int j=1;j<=n;j++)
106         {
107             if(map[j]<=mid && map[j]!=0&&map[j]<MAX&&i!=j) //
108             {
109                 add_edge(i,n+j,INF);
110                 add_edge(n+j,i,0);
111             }
112         }
113     if(sap()==ans)
114         return 1;
115     else return 0;
116 }
117
118 int main()
119 {
120     s=0;
121     scanf("%d%d",&n,&m);
122     t=2*n+1;
123     for(int i=1;i<=n;i++)
124     {
125         int x,y;
126         scanf("%d%d",&x,&y);
127         ans+=x;
128         savex=x;
129         savey=y;
130     }
131     for(int i=1;i<=n;i++)
132         for(int j=1;j<=n;j++)
133             if(i!=j)
134             {
135                 map[j]=MAX; //
136             }
137             else
138                 map[j]=0;
139
140     for(int i=0;i<m;i++)
141     {
142         scanf("%d%d%I64d",&g.u,&g.v,&g.w);
143         if(map[g.u][g.v]>g.w)
144         {
145             map[g.u][g.v] = map[g.v][g.u]=g.w;
146         }
147     }
148     for(int k=1;k<=n;k++)
149         for(int i=1;i<=n;i++)
150             for(int j=1;j<=n;j++)
151             {
152                 if(map[j]>map[k]+map[k][j])
153                 {
154                     map[j]=map[k]+map[k][j];
155                 }
156             }
157     __int64 b,d;
158     b=0;
159     d=50000001000000LL; // 最大的距离,1000000000*200
160     if(check(d)!=1)
161     {
162         printf("-1");
163         return 0;
164     }
165     while(b<d)
166     {
167         __int64 mid=(b+d)/2;
168         if(check(mid)==1)
169         {
170             d=mid;
171         }
172         else
173         {
174             b=mid+1;
175         }
176     }
177     printf("%I64d",b);
178     printf("\n");
179     return 0;
180 }
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-114974-1-1.html 上篇帖子: SAP收货时自动创建采购订单(转) 下篇帖子: 学习SAP感悟
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表