poj 3155 Hard Life(01分数规划+最大流--最大密度子图)
2017-08-24 17:34
656 查看
Hard Life
Description
John is a Chief Executive Officer at a privately owned medium size company. The owner of the company has decided to make his son Scott a manager in the company. John fears that the owner will ultimately give CEO position to Scott if he does well on his new
manager position, so he decided to make Scott’s life as hard as possible by carefully selecting the team he is going to manage in the company.
John knows which pairs of his people work poorly in the same team. John introduced a hardness factor of a team — it is a number of pairs of people from this team who work poorly in the same team divided by the total number of people in the team.
The larger is the hardness factor, the harder is this team to manage. John wants to find a group of people in the company that are hardest to manage and make it Scott’s team. Please, help him.
In the example on the picture the hardest team consists of people 1, 2, 4, and 5. Among 4 of them 5 pairs work poorly in the same team, thus hardness factor is equal to 5⁄4. If we add person number 3 to the team then hardness factor
decreases to 6⁄5.
Input
The first line of the input file contains two integer numbers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 1000). Here n is a total number of people in the company (people are numbered from 1 to n), and m is the number
of pairs of people who work poorly in the same team. Next m lines describe those pairs with two integer numbers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi)
on a line. The order of people in a pair is arbitrary and no pair is listed twice.
Output
Write to the output file an integer number k (1 ≤ k ≤ n) — the number of people in the hardest team, followed by k lines listing people from this team in ascending order. If there are multiple teams with the same hardness
factor then write any one.
Sample Input
Sample Output
经典问题:
题意:给你n个点m条边的无向图,找到一个点个数/边个数最小的子图,并输出子图大小和在这个子图中的所有点
有多个答案输出任意一种
先说下思路/过程:
①二分答案,初始下界0,初始上界m,当它们的差小于1/n²时结束,所有流量不为0的边都在最大密度子图中
②假设当前二分值mid,每个点的度数为in[],那么建图求最大流
如果a和b两点之间有边,那么a到b和b到a各连接一条容量为1的边
源点向所有点连接一条容量为m的边,所有点向汇点连接一条容量为m+2*mid-in[i]的边
③如果最大流小于n*m(没有满流),说明存在更优解
m=0时特判,还有二分结束之后尽量拿L再跑一次最大流
这道题在《挑战程序设计》上面有,但难度真的不低,特别是证明
并不是怎么看懂,这里就引用下吧
http://www.hankcs.com/program/algorithm/poj-3155-hard-life.html
Time Limit: 8000MS | Memory Limit: 65536K | |
Total Submissions: 9173 | Accepted: 2668 | |
Case Time Limit: 2000MS | Special Judge |
John is a Chief Executive Officer at a privately owned medium size company. The owner of the company has decided to make his son Scott a manager in the company. John fears that the owner will ultimately give CEO position to Scott if he does well on his new
manager position, so he decided to make Scott’s life as hard as possible by carefully selecting the team he is going to manage in the company.
John knows which pairs of his people work poorly in the same team. John introduced a hardness factor of a team — it is a number of pairs of people from this team who work poorly in the same team divided by the total number of people in the team.
The larger is the hardness factor, the harder is this team to manage. John wants to find a group of people in the company that are hardest to manage and make it Scott’s team. Please, help him.
In the example on the picture the hardest team consists of people 1, 2, 4, and 5. Among 4 of them 5 pairs work poorly in the same team, thus hardness factor is equal to 5⁄4. If we add person number 3 to the team then hardness factor
decreases to 6⁄5.
Input
The first line of the input file contains two integer numbers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 1000). Here n is a total number of people in the company (people are numbered from 1 to n), and m is the number
of pairs of people who work poorly in the same team. Next m lines describe those pairs with two integer numbers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi)
on a line. The order of people in a pair is arbitrary and no pair is listed twice.
Output
Write to the output file an integer number k (1 ≤ k ≤ n) — the number of people in the hardest team, followed by k lines listing people from this team in ascending order. If there are multiple teams with the same hardness
factor then write any one.
Sample Input
sample input #1 5 6 1 5 5 4 4 2 2 5 1 2 3 1 sample input #2 4 0
Sample Output
sample output #1 4 1 2 4 5 sample output #2 1 1
经典问题:
题意:给你n个点m条边的无向图,找到一个点个数/边个数最小的子图,并输出子图大小和在这个子图中的所有点
有多个答案输出任意一种
先说下思路/过程:
①二分答案,初始下界0,初始上界m,当它们的差小于1/n²时结束,所有流量不为0的边都在最大密度子图中
②假设当前二分值mid,每个点的度数为in[],那么建图求最大流
如果a和b两点之间有边,那么a到b和b到a各连接一条容量为1的边
源点向所有点连接一条容量为m的边,所有点向汇点连接一条容量为m+2*mid-in[i]的边
③如果最大流小于n*m(没有满流),说明存在更优解
m=0时特判,还有二分结束之后尽量拿L再跑一次最大流
这道题在《挑战程序设计》上面有,但难度真的不低,特别是证明
并不是怎么看懂,这里就引用下吧
http://www.hankcs.com/program/algorithm/poj-3155-hard-life.html
#include<stdio.h> #include<queue> #include<string.h> using namespace std; int k, cnt, S, T, head[105], h[105], cur[105], road[105][105], in[105], vis[105]; typedef struct { int to, next; double flow; }Road; Road G[5005]; void Add(int u, int v, double flow) { cnt++; G[cnt].next = head[u]; head[u] = cnt; G[cnt].to = v; G[cnt].flow = flow; } int Jud() { int now, i; queue<int> q; memset(h, -1, sizeof(h)); q.push(S); h[S] = 0; while(q.empty()==0) { now = q.front(); q.pop(); for(i=head[now];i!=0;i=G[i].next) { if(G[i].flow && h[G[i].to]==-1) { h[G[i].to] = h[now]+1; q.push(G[i].to); } } } if(h[T]!=-1) return 1; return 0; } double Sech(int x, double flow) { int i; double w, used; if(x==T) return flow; used = 0; for(i=cur[x];i!=0;i=G[i].next) { if(h[G[i].to]==h[x]+1) { w = Sech(G[i].to, min(flow-used, G[i].flow)); G[i].flow -= w; G[i^1].flow += w; if(G[i].flow) cur[x] = i; used += w; if(used==flow) return flow; } } if(used==0) h[x] = -1; return used; } double Dinic() { int i; double flow = 0; while(Jud()) { for(i=S;i<=T;i++) cur[i] = head[i]; flow += Sech(S, 1<<25); } return flow; } void Rest(int u) { int i; vis[u] = 1, k += 1; for(i=head[u];i!=0;i=G[i].next) { if(vis[G[i].to]==0 && G[i].flow>0) Rest(G[i].to); } } int main(void) { int n, m, x, y, i, j; double l, r, mid, ans; while(scanf("%d%d", &n, &m)!=EOF) { if(m==0) { printf("1\n1\n"); continue; } memset(road, 0, sizeof(road)); memset(in, 0, sizeof(in)); for(i=1;i<=m;i++) { scanf("%d%d", &x, &y); road[x][y] = road[y][x] = 1; in[x]++, in[y]++; } l = 0, r = m; S = 0, T = n+1; while(n*n*(r-l)>1) { cnt = 1; memset(head, 0, sizeof(head)); mid = (l+r)/2; for(i=1;i<=n;i++) { Add(S, i, m); Add(i, S, 0); Add(i, T, m+2*mid-in[i]); Add(T, i, 0); } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(road[i][j]) { Add(i, j, 1); Add(j, i, 0); } } } ans = n*m-Dinic(); if(ans>1e-5) l = mid; else r = mid; } cnt = 1; memset(head, 0, sizeof(head)); for(i=1;i<=n;i++) { Add(S, i, m); Add(i, S, 0); Add(i, T, m+2*l-in[i]); Add(T, i, 0); } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(road[i][j]) { Add(i, j, 1); Add(j, i, 0); } } } Dinic(); k = 0; memset(vis, 0, sizeof(vis)); Rest(0); printf("%d\n", k-1); for(i=1;i<=n;i++) { if(vis[i]) printf("%d\n", i); } } return 0; }
相关文章推荐
- POJ-3155-Hard Life(最大密度子图)(01分数规划+最小割)
- poj 3155 Hard Life(最大密度子图,01分数规划)
- poj 3155(Hard Life)分数规划/最大密度子图
- 最大密集子图(01分数规划+二分+最小割)POJ3155
- poj 3155 01规划->最大密度密度子图->最大流
- 最大密集子图(01分数规划+二分+最小割)POJ3155
- poj 3155 Hard Life 【最大密度子图】 【0-1分数规划 + 最小割】
- 【POJ】3155 Hard Life 最大密度子图
- POJ 3155 Hard Life 最大密度子图
- POJ 3155 Hard Life(最大密度子图)
- POJ 3155 Hard Life(最大密度子图)
- poj 3155 Hard Life 最大密度子图
- 【01分数规划】最大密度子图
- POJ 3155 Hard Life 最大密度子图
- POJ 3155 Hard Life(最大密度子图)
- poj 3155 Hard Life 网络流——最大密度子图
- POJ 3155 Hard Life(最大密度子图)
- POJ 3155 Hard Life(最大密度子图)
- poj 3155 Hard Life (最大密度子图)
- POJ 3155 Hard Life 最大密度子图