hust 1230 beautiful

题目描述

Timy is visiting a beautiful park. There are M rivers and N lakes(marked
1-N), any two lakes are connected by at most one river. He knows that the ith
river had to have water flowed with speed in the range [li,ri] to make the park
beautiful . The staff will pour water in one lake (lake marked 1) and only one
lake (lake marked N) connected to the outside from which the water will flow
away. To save water , Timy wants to know what‘s the minimal speed for the staff
to pour water in order to make the park beautiful .

输入

The input contains several test cases. For each test case Two positive
integer numbers N (1 <= N <= 60) and M (0 < M <= N^2) have been
written in the first line - number of lakes and rivers. There are M lines
follows: each line contains four integer numbers fi, ti, li, ri (fi!=ti , 0 <
fi,ti <= N, 0 <= li <= ri < 10000); the numbers are separated by
space indicate the ith river flow from fi to ti and the range of speed is
[li,ri].

输出

Write one integer number for each test case - it ought to be the minimal
speed of add water. If it is impossible to make the park beautiful, write
"Impossible".

样例输入

4 4
1 2 0 2
2 4 1 1
1 3 2 2
3 4 0 3
4 4
1 2 0 1
2 4 2 2
1 3 3 3
3 4 0 2

样例输出

3
Impossible

这个题目,知道网络流的人,一看就知道是一道有流量上下界的最小流问题
解法,添加一个超级源点和一个超级汇点,构造出一个只含有自由流和必须流的图,求源汇点的最大流flow1,再添加一条从n到1的容量为inf的边,再求一次最大流flow2,这时候就可以判断了,若flow1+flow2==总的必须流,那么有解,解就是n到1的流量,否则无解,这样就避免了做删边的操作,而且简单多了,时间效率也不错


#include<map>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define inf 0x0f0f0f0f

using namespace std;

const double pi=acos(-1.0);
const double eps=1e-8;
typedef pair<int,int>pii;

const int maxn=60+10;

struct Edge
{
int from,to,cap,flow;
};

int n,m,s,t;
vector<Edge>edges;
vector<int>G[maxn];
int d[maxn],cur[maxn];
bool vis[maxn];

void AddEdge(int from,int to,int cap)
{
Edge temp;
temp.cap=cap; temp.flow=0; temp.from=from; temp.to=to;
edges.push_back(temp);
temp.cap=0; temp.flow=0; temp.from=to; temp.to=from;
edges.push_back(temp);
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}

bool BFS()
{
memset(vis,0,sizeof(vis));
queue<int>Q;
Q.push(s);
d[s]=0;
vis[s]=1;
while(!Q.empty())
{
int x=Q.front();Q.pop();
for (int i=0;i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if (!vis[e.to] && e.cap>e.flow)
{
vis[e.to]=1;
d[e.to]=d[x]+1;
Q.push(e.to);
}
}
}
return vis[t];
}

int DFS(int x,int a)
{
if (x==t || a==0) return a;
int flow=0,f;
for (int& i=cur[x];i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if (d[x]+1==d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0)
{
e.flow+=f;
edges[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if (a==0) break;
}
}
return flow;
}

int Dinic()
{
int flow=0;
while (BFS())
{
memset(cur,0,sizeof(cur));
flow+=DFS(s,inf);
}
return flow;
}

void init()
{
for (int i=0;i<=maxn;i++) G[i].clear();
edges.clear();
}

int main()
{
int N,M,indegree[maxn],outdegree[maxn],x,y,down,up,sum;
while(scanf("%d%d",&N,&M)!=EOF)
{
memset(indegree,0,sizeof(indegree));
memset(outdegree,0,sizeof(outdegree));
sum=0;
n=N+2;
init();
for (int i=0;i<M;i++)
{
scanf("%d%d%d%d",&x,&y,&down,&up);
AddEdge(x,y,up-down);
indegree[y]+=down;
outdegree[x]+=down;
}
for (int i=1;i<=N;i++)
{
int mi=indegree[i]-outdegree[i];
if (mi>0) AddEdge(0,i,mi);
if (mi<0) {AddEdge(i,N+1,-mi);sum+=(-mi);}
}
s=0; t=N+1;
//AddEdge(N,1,inf);
int ans1=Dinic();
AddEdge(N,1,inf);
int ans2=Dinic();
if (ans1+ans2!=sum) printf("Impossible\n");
else
{
Edge temp=edges[m-2];
printf("%d\n",temp.flow);
}
}
return 0;
}

作者 chensunrise

时间: 2024-12-23 00:27:13

hust 1230 beautiful的相关文章

hust 1164 4 Rain on your Parade

题目描述 You're giving a party in the garden of your villa by the sea. The party is a huge success, and everyone is here. It's a warm, sunny evening, and a soothing wind sends fresh, salty air from the sea. The evening is progressing just as you had imag

第十四届华中科技大学程序设计竞赛决赛同步赛 Beautiful Land

It's universally acknowledged that there're innumerable trees in the campus of HUST.Now HUST got a big land whose capacity is C to plant trees. We have n trees which could be plant in it. Each of the trees makes HUST beautiful which determined by the

HUST 1588 辗转数对

1588 - 辗转数对 时间限制:1秒 内存限制:128兆 155 次提交 27 次通过 题目描述 假设当前有一个数对(a, b),我们可以通过一步将这个数对变为一个新数对(a + b, b)或者是(a, a + b).初始的数对为(1, 1),你的任务是找到一个数字k,即通过最少的步数使得这个数对中至少一个数字等于n. 输入 输入包括多组数据,每组数据包括一行,每行有一个整数n. 输出 每组数据输出一行,每行一个整数n. 样例输入 5 3 样例输出 3 2 提示 第一个样例的方法是 (1,1)

HUST 1698 - 电影院 组合数学 + 分类思想

http://acm.hust.edu.cn/problem/show/1698 题目就是要把一个数n分成4段,其中中间两段一定要是奇数. 问有多少种情况. 分类, 奇数 + 奇数 + 奇数 + 奇数 奇数 + 奇数 + 奇数 + 偶数 偶数 + 奇数 + 奇数 + 奇数 偶数 + 奇数 + 奇数 + 偶数 然后奇数表达成 2 * a - 1这个样子,就能列出方程. 然后就是类似于解a1 + a2 + a3 + a4 = x的问题了. #include <cstdio> #include &l

[Python]HTML/XML解析器Beautiful Soup

[简介] Beautiful Soup是一个可以从HTML或XML文件中提取数据的Python库.即HTML/XMLX的解析器. 它可以很好的处理不规范标记并生成剖析树(parse tree). 它提供简单又常用的导航(navigating),搜索以及修改剖析树的操作.它可以大大节省你的编程时间. [安装] 下载地址:点击打开链接 Linux平台安装: 如果你用的是新版的Debain或ubuntu,那么可以通过系统的软件包管理来安装: $ apt-get install Python-bs4 B

HUST 1017 - Exact cover (Dancing Links 模板题)

1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in exactly one of the selected rows. Try to find o

python标准库Beautiful Soup与MongoDb爬喜马拉雅电台的总结

Beautiful Soup标准库是一个可以从HTML/XML文件中提取数据的Python库,它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式,Beautiful Soup将会节省数小时的工作时间.pymongo标准库是MongoDb NoSql数据库与python语言之间的桥梁,通过pymongo将数据保存到MongoDb中.结合使用这两者来爬去喜马拉雅电台的数据... Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,其中一个是

勇士出征[HUST 1439]

勇士出征[HUST 1439] 时间1000ms,内存64MB 第十届"北大青鸟"杯浙江师范大学程序设计竞赛 这道题跟UVA-12100是一样的题目.我这里用的是STL的双端队列deque容器配合优先队列priority_queue,写起来会比较轻松:依次将输入压入队列,然后不断扫描队列,符合最大优先级的(优先队列的顶部元素)将其送出,而不再压入队尾.直到找到符合自己的标记的为止. 当然这道题也有用数组使用滚雪球的方式实现的,也就是开一个大的数组,每次将元素后挪时,直接将其放在数组末尾

[2016-03-08][651][codeforces][B][Beautiful Paintings]

[2016-03-08][651][[codeforces]][B][Beautiful Paintings] 题目编号:CF 651 B 题目大意:画展有n个画,每个画beauty值已知,客人每经过一个比之前一个画更美的,客人会变得beauty,重新安排画的顺序,求客人开心时间的最大值 输入:n 和每个话的beauty 输出:时间 分析: 如果beauty为a的画,数量为 k,那么beaty小于或者大于a的k个,总能安排让客人开心,所以,除了数量k最大的那个beauty之外,每个beauty总