poj 2391 二分 拆点 最大值最小值网络流
2015-12-18 21:58
597 查看
问题:有若干块草地,有的草地有棚子 有的没有 棚子有容量 给牛避雨.草地与草地之间有路,如果要下雨则要发出警报,问最短多长时间需要发出警报,保证所有牛都不被淋湿.
还是先Floyd,然后二分求最大值的最小值.这里构图需要注意 要拆点.一个点可能既有牛(源点与之相连,容量为当前有牛的数量).也有棚子(与汇点相连,容量为棚子的容量)
有牛的点 指向所有该点能够到达的有棚子的点,容量为无穷大.
拆点的原因: 待补充
注意!!!!!! 数组没开够大的话 他有可能会报WA的!!!不一定是RE 注意注意!!!
/*
* Dinic algo for max flow
*
* This implementation assumes that #nodes, #edges, and capacity on each edge <= INT_MAX,
* which means INT_MAX is the best approximation of INF on edge capacity.
* The total amount of max flow computed can be up to LLONG_MAX (not defined in this file),
* but each 'dfs' call in 'dinic' can return <= INT_MAX flow value
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <assert.h>
#include <queue>
#include <vector>
#include <algorithm>
#include<iostream>
#define INF -1
#define N (3000)
#define M (222222)
typedef long long LL;
using namespace std;
struct edge {
int v, cap, next;
};
edge e[M];
int head
, level
, cur
;
int num_of_edges;
/*
* When there are multiple test sets, you need to re-initialize before each
*/
void dinic_init(void) {
num_of_edges = 0;
memset(head, -1, sizeof(head));
return;
}
int add_edge(int u, int v, int c1, int c2) {
int& i=num_of_edges;
assert(c1>=0 && c2>=0 && c1+c2>=0); // check for possibility of overflow
e[i].v = v;
e[i].cap = c1;
e[i].next = head[u];
head[u] = i++;
e[i].v = u;
e[i].cap = c2;
e[i].next = head[v];
head[v] = i++;
return i;
}
void print_graph(int n) {
for (int u=0; u<n; u++) {
printf("%d: ", u);
for (int i=head[u]; i>=0; i=e[i].next) {
printf("%d(%d)", e[i].v, e[i].cap);
}
printf("\n");
}
return;
}
/*
* Find all augmentation paths in the current level graph
* This is the recursive version
*/
int dfs(int u, int t, int bn) {
if (u == t) return bn;
int left = bn;
for (int i=cur[u]; i>=0; i=e[i].next) {
int v = e[i].v;
int c = e[i].cap;
if (c > 0 && level[u]+1 == level[v]) {
int flow = dfs(v, t, min(left, c));
if (flow > 0) {
e[i].cap -= flow;
e[i^1].cap += flow;
cur[u] = i;
left -= flow;
if (!left) break;
}
}
}
if (left > 0) level[u] = 0;
return bn - left;
}
bool bfs(int s, int t) {
memset(level, 0, sizeof(level));
level[s] = 1;
queue<int> q;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
if (u == t) return true;
for (int i=head[u]; i>=0; i=e[i].next) {
int v = e[i].v;
if (!level[v] && e[i].cap > 0) {
level[v] = level[u]+1;
q.push(v);
}
}
}
return false;
}
LL dinic(int s, int t) {
LL max_flow = 0;
while (bfs(s, t)) {
memcpy(cur, head, sizeof(head));
max_flow += dfs(s, t, INT_MAX);
}
return max_flow;
}
int upstream(int s, int n) {
int cnt = 0;
vector<bool> visited(n);
queue<int> q;
visited[s] = true;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i=head[u]; i>=0; i=e[i].next) {
int v = e[i].v;
if (e[i].cap > 0 && !visited[v]) {
visited[v] = true;
q.push(v);
cnt++;
}
}
}
return cnt; // excluding s
}
LL f[555][555];
int cNumArr[555];
int cCapArr[555];
int F,P;
int sumS;
void create_graph(LL limit)
{
dinic_init();
for(int i=1;i<=F;i++)
{
add_edge(0,i,cNumArr[i],0);
add_edge(i+F,2*F+1,cCapArr[i],0);
}
for(int i=1;i<=F;i++)
{
for(int j=1;j<=F;j++)
{
// if(cNumArr[i] >0)
// {
if(f[i][j]<=limit &&f[i][j]!=INF)
{
add_edge(i,F+j,sumS,0);
}
// }
/*
if(cCapArr[j]>0)
{
if(f[i][j]<=limit)
{
add_edge(i,j,sumS,0);
}
} */
}
}
}
int main() {
cin>>F>>P;
memset(f,0,sizeof(f));
sumS = 0;
for(int i=1;i<=F;i++)
{
scanf("%d %d",&cNumArr[i],&cCapArr[i]);
sumS+=cNumArr[i];
}
for(int i=1;i<=P;i++)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
if(f[a][b]==0)
{
f[a][b] = c;
f[b][a] = c;
}
else if(f[a][b]>c)
{
f[a][b] = c;
f[b][a] = c;
}
}
for(int i=1;i<=F;i++)
{
for(int j=1;j<=F;j++)
{
if(f[i][j]==0 && i!=j)
{
f[i][j] = INF;
}
}
}
for(int k=1;k<=F;k++)
{
for(int i=1;i<=F;i++)
{
for(int j=1;j<=F;j++)
{
if(f[i][j] == INF)
{
if(f[i][k] != INF && f[k][j]!=INF)
{
f[i][j] = f[i][k]+f[k][j];
}
}
else
{
if(f[i][j]>f[i][k]+f[k][j] && f[i][k] != INF && f[k][j]!=INF)
{
f[i][j] = f[i][k]+f[k][j];
}
}
}
}
}
/*
cout<<"----"<<endl;
for(int i=1;i<=F;i++)
{
for(int j=1;j<=F;j++)
{
cout<<f[i][j]<<" ";
}
cout<<endl;
}*/
LL l=0,r=0,mid,ans=-1;
for(int i=1;i<=F;i++)
{
for(int j=1;j<=F;j++)
{
if(r<f[i][j] && f[i][j]!=INF)
{
r = f[i][j];
}
}
}
while(l<=r)
{
mid = (l+r)/2;
create_graph(mid);
int flow = dinic(0,2*F+1);
// cout<<mid<<" "<<flow <<" "<<sumS<<endl;
if(flow==sumS)
{
ans = mid;
r = mid-1;
}
else
{
l = mid+1;
}
}
cout<<ans<<endl;
}
还是先Floyd,然后二分求最大值的最小值.这里构图需要注意 要拆点.一个点可能既有牛(源点与之相连,容量为当前有牛的数量).也有棚子(与汇点相连,容量为棚子的容量)
有牛的点 指向所有该点能够到达的有棚子的点,容量为无穷大.
拆点的原因: 待补充
注意!!!!!! 数组没开够大的话 他有可能会报WA的!!!不一定是RE 注意注意!!!
/*
* Dinic algo for max flow
*
* This implementation assumes that #nodes, #edges, and capacity on each edge <= INT_MAX,
* which means INT_MAX is the best approximation of INF on edge capacity.
* The total amount of max flow computed can be up to LLONG_MAX (not defined in this file),
* but each 'dfs' call in 'dinic' can return <= INT_MAX flow value
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <assert.h>
#include <queue>
#include <vector>
#include <algorithm>
#include<iostream>
#define INF -1
#define N (3000)
#define M (222222)
typedef long long LL;
using namespace std;
struct edge {
int v, cap, next;
};
edge e[M];
int head
, level
, cur
;
int num_of_edges;
/*
* When there are multiple test sets, you need to re-initialize before each
*/
void dinic_init(void) {
num_of_edges = 0;
memset(head, -1, sizeof(head));
return;
}
int add_edge(int u, int v, int c1, int c2) {
int& i=num_of_edges;
assert(c1>=0 && c2>=0 && c1+c2>=0); // check for possibility of overflow
e[i].v = v;
e[i].cap = c1;
e[i].next = head[u];
head[u] = i++;
e[i].v = u;
e[i].cap = c2;
e[i].next = head[v];
head[v] = i++;
return i;
}
void print_graph(int n) {
for (int u=0; u<n; u++) {
printf("%d: ", u);
for (int i=head[u]; i>=0; i=e[i].next) {
printf("%d(%d)", e[i].v, e[i].cap);
}
printf("\n");
}
return;
}
/*
* Find all augmentation paths in the current level graph
* This is the recursive version
*/
int dfs(int u, int t, int bn) {
if (u == t) return bn;
int left = bn;
for (int i=cur[u]; i>=0; i=e[i].next) {
int v = e[i].v;
int c = e[i].cap;
if (c > 0 && level[u]+1 == level[v]) {
int flow = dfs(v, t, min(left, c));
if (flow > 0) {
e[i].cap -= flow;
e[i^1].cap += flow;
cur[u] = i;
left -= flow;
if (!left) break;
}
}
}
if (left > 0) level[u] = 0;
return bn - left;
}
bool bfs(int s, int t) {
memset(level, 0, sizeof(level));
level[s] = 1;
queue<int> q;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
if (u == t) return true;
for (int i=head[u]; i>=0; i=e[i].next) {
int v = e[i].v;
if (!level[v] && e[i].cap > 0) {
level[v] = level[u]+1;
q.push(v);
}
}
}
return false;
}
LL dinic(int s, int t) {
LL max_flow = 0;
while (bfs(s, t)) {
memcpy(cur, head, sizeof(head));
max_flow += dfs(s, t, INT_MAX);
}
return max_flow;
}
int upstream(int s, int n) {
int cnt = 0;
vector<bool> visited(n);
queue<int> q;
visited[s] = true;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i=head[u]; i>=0; i=e[i].next) {
int v = e[i].v;
if (e[i].cap > 0 && !visited[v]) {
visited[v] = true;
q.push(v);
cnt++;
}
}
}
return cnt; // excluding s
}
LL f[555][555];
int cNumArr[555];
int cCapArr[555];
int F,P;
int sumS;
void create_graph(LL limit)
{
dinic_init();
for(int i=1;i<=F;i++)
{
add_edge(0,i,cNumArr[i],0);
add_edge(i+F,2*F+1,cCapArr[i],0);
}
for(int i=1;i<=F;i++)
{
for(int j=1;j<=F;j++)
{
// if(cNumArr[i] >0)
// {
if(f[i][j]<=limit &&f[i][j]!=INF)
{
add_edge(i,F+j,sumS,0);
}
// }
/*
if(cCapArr[j]>0)
{
if(f[i][j]<=limit)
{
add_edge(i,j,sumS,0);
}
} */
}
}
}
int main() {
cin>>F>>P;
memset(f,0,sizeof(f));
sumS = 0;
for(int i=1;i<=F;i++)
{
scanf("%d %d",&cNumArr[i],&cCapArr[i]);
sumS+=cNumArr[i];
}
for(int i=1;i<=P;i++)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
if(f[a][b]==0)
{
f[a][b] = c;
f[b][a] = c;
}
else if(f[a][b]>c)
{
f[a][b] = c;
f[b][a] = c;
}
}
for(int i=1;i<=F;i++)
{
for(int j=1;j<=F;j++)
{
if(f[i][j]==0 && i!=j)
{
f[i][j] = INF;
}
}
}
for(int k=1;k<=F;k++)
{
for(int i=1;i<=F;i++)
{
for(int j=1;j<=F;j++)
{
if(f[i][j] == INF)
{
if(f[i][k] != INF && f[k][j]!=INF)
{
f[i][j] = f[i][k]+f[k][j];
}
}
else
{
if(f[i][j]>f[i][k]+f[k][j] && f[i][k] != INF && f[k][j]!=INF)
{
f[i][j] = f[i][k]+f[k][j];
}
}
}
}
}
/*
cout<<"----"<<endl;
for(int i=1;i<=F;i++)
{
for(int j=1;j<=F;j++)
{
cout<<f[i][j]<<" ";
}
cout<<endl;
}*/
LL l=0,r=0,mid,ans=-1;
for(int i=1;i<=F;i++)
{
for(int j=1;j<=F;j++)
{
if(r<f[i][j] && f[i][j]!=INF)
{
r = f[i][j];
}
}
}
while(l<=r)
{
mid = (l+r)/2;
create_graph(mid);
int flow = dinic(0,2*F+1);
// cout<<mid<<" "<<flow <<" "<<sumS<<endl;
if(flow==sumS)
{
ans = mid;
r = mid-1;
}
else
{
l = mid+1;
}
}
cout<<ans<<endl;
}
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析