UVAlive2531 The K-League(最大流)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33451
【思路】
最大流。
大体思路是枚举每个队伍,最大流判断是否可能成为冠军。
构图:
1 建立ST,比赛(u,v)建立n^2个结点,队伍u建立n个结点。
2 由S向(u,v)连容量为a[u][v]的边,由(u,v)向u和v连容量为INF的边,由u向T连容量为total-w[u]的边。
3 如果从S发出的边都满载则该team可行。
【代码】
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#define FOR(a,b,c) for(int a=(b);a<(c);a++)
using namespace std; const int maxn = +;
const int INF = 1e9; struct Edge{
int u,v,cap,flow;
};
struct Dinic {
int n,m,s,t;
bool vis[maxn];
int d[maxn],cur[maxn];
vector<int> G[maxn];
vector<Edge> es; void init(int n) {
this->n=n;
es.clear();
for(int i=;i<n;i++) G[i].clear();
}
void AddEdge(int u,int v,int cap) {
es.push_back((Edge){u,v,cap,});
es.push_back((Edge){v,u,,});
m=es.size();
G[u].push_back(m-);
G[v].push_back(m-);
} bool BFS() {
queue<int> q;
memset(vis,,sizeof(vis));
q.push(s); vis[s]=; d[s]=;
while(!q.empty()) {
int u=q.front(); q.pop();
for(int i=;i<G[u].size();i++) {
Edge& e=es[G[u][i]];
int v=e.v;
if(!vis[v] && e.cap>e.flow) {
vis[v]=;
d[v]=d[u]+;
q.push(v);
}
}
}
return vis[t];
}
int DFS(int u,int a) {
if(u==t || a==) return a;
int flow=,f;
for(int& i=cur[u];i<G[u].size();i++){
Edge& e=es[G[u][i]];
int v=e.v;
if( d[v]==d[u]+ && (f=DFS(v,min(a,e.cap-e.flow)))> ) {
e.flow+=f;
es[G[u][i]^].flow-=f;
flow+=f,a-=f;
if(!a) break;
}
}
return flow;
}
int Maxflow(int s,int t) {
this->s=s , this->t=t;
int flow=;
while(BFS()) {
memset(cur,,sizeof(cur));
flow+=DFS(s,INF);
}
return flow;
}
} dc; int n;
int w[maxn],d[maxn],a[maxn][maxn]; int main() {
int T;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
for(int i=;i<n;i++) scanf("%d%d",&w[i],&d[i]);
int sum=;
for(int i=;i<n;i++)
for(int j=;j<n;j++) scanf("%d",&a[i][j]) , sum+=a[i][j];
sum/=;
int S=n*n+n , T=S+;
bool first=;
for(int team=;team<n;team++)
{
int total=w[team];
for(int i=;i<n;i++) total+=a[team][i];
bool flag=;
for(int i=;i<n;i++) if(w[i]>total) flag=; //即使全胜依然不可能是冠军
if(flag) continue;
dc.init(n*n+n+);
for(int i=;i<n;i++) {
for(int j=i+;j<n;j++) {
int r=i*n+j;
if(a[i][j]) dc.AddEdge(S,r,a[i][j]);
dc.AddEdge(r,n*n+i,INF) , dc.AddEdge(r,n*n+j,INF);
}
dc.AddEdge(n*n+i,T,total-w[i]);
}
if(dc.Maxflow(S,T)==sum) {
if(first) first=; else putchar(' ');
printf("%d",team+);
}
}
putchar('\n');
}
return ;
}
UVAlive2531 The K-League(最大流)的更多相关文章
- POJ - 2516 Minimum Cost 每次要跑K次费用流
传送门:poj.org/problem?id=2516 题意: 有m个仓库,n个买家,k个商品,每个仓库运送不同商品到不同买家的路费是不同的.问为了满足不同买家的订单的最小的花费. 思路: 设立一个源 ...
- poj-2516.minimum cost(k次费用流)
Minimum Cost Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19883 Accepted: 7055 Des ...
- UVALive-2531 The K-League (最大流建模+枚举)
题目大意:有n支足球队,已知每支球队的已胜场数和任意两支球队之间还需要的比赛场数a[i][j],求最终可能夺冠的所有球队. 题目分析:枚举所有的球队,对于球队 i 让它在接下来的比赛中全部获胜,如果这 ...
- hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙
/** 题目:hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4106 ...
- 【wikioi】1034 家园(最大流+特殊的技巧)
http://wikioi.com/problem/1034/ 太神了这题. 其实一开始我以为是费用流,但是总感觉不对. 原因是我没看到一句话,特定的时刻到达特定的点!! 也就是说,并不是每艘船每次都 ...
- BZOJ 2324 营救皮卡丘(最小费用最大流)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2324 题意:n+1个城市(0到n).初始时K个 人都在0城市.城市之间有距离.要求(1) ...
- hdu3081 Marriage Match II(二分+并查集+最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意: n个女生与n个男生配对,每个女生只能配对某些男生,有些女生相互是朋友,每个女生也可以跟她 ...
- POJ 2516 Minimum Cost (费用流)
题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...
- 【费用流】【CODEVS】1227 方格取数2
[算法]最小费用最大流(费用流) [题解] 费用流:http://www.cnblogs.com/onioncyc/p/6496532.html 本题构图: 在有限的k次行走中尽可能多的拿到数字,明显 ...
随机推荐
- CSS美化 input type=file 兼容各个浏览器(转)
HTML代码: <FORM> <A class=btn_addPic href="javascript:void(0);"><SPAN>< ...
- TSQL Beginners Challenge 1 - Find the second highest salary for each department
很久以前准备写的系列文章,后来因为懒一直耽搁着,今天突然决定继续下去,于是有了这篇文章,很基础,但很常用.题目描述依然拷贝.简单来说就是找出个个部门薪水排名第二的人,排名相同的要一起列出来. Intr ...
- An App ID with Identifier 'xxxxxx’ is not available. Please ....
1.完全关闭Xcode; 2.找到钥匙串,将钥匙串(Keychain)中的对应证书移除: 3.再次打开Xcode,通过 Preferences - Account 4. 删除原先的账号重新登录, 搞定 ...
- CoreMotion(加速计)
加速计的作用 用于检测设备的运动(比如摇晃) 加速计的经典应用场景 摇一摇 计步器 ********************************** Core Motion获取数据的两种方式 pu ...
- 104. Maximum Depth of Binary Tree(C++)
104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is ...
- SGU 144.Meeting
题目: 两支地区ACM比赛的队伍决定为了国际决赛而在一起集训. 他们约定在某天的 X 时到 Y 时的某一时刻相会. 但由于他们很少按时到 (有的队伍比赛那天都会迟到), 他们没有设定一个确切的相遇时间 ...
- 【POJ2985】【Treap + 并查集】The k-th Largest Group
Description Newman likes playing with cats. He possesses lots of cats in his home. Because the numbe ...
- xubuntu14.04截图,彻底到Linux一个半月后记
前言 自学计算机技术,越到后面,越依赖ubuntu,以致于很多时候都是一开机就打开虚拟机上的ubuntu10.04,Linux已经变得越来越重要了. 2014-04-17,ubuntu14.0 ...
- 给id赋值
var div = document.getElementByTagName('div') div.id="mydiv";div.setAttribute("id&quo ...
- C# 控制台程序设置字体颜色
这几天做了个程序,程序本身很简单.大体功能是输入查询条件,从数据库里取出结果计算并显示.但是用户的要求是使用控制台(console)来实现功能.由于功能简单,程序很快就做完了,在面向用户演示程序时,突 ...