POJ 3155 Hard Life
Hard Life
This problem will be judged on PKU. Original ID: 3155
64-bit integer IO format: %lld Java class name: Main
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
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,next;
double flow;
arc(int x = ,double y = ,int z = -){
to = x;
flow = y;
next = z;
}
};
arc e[maxn<<];
int head[maxn],d[maxn],cur[maxn],x[maxn],y[maxn];
int tot,S,T,n,m;
void add(int u,int v,double flow){
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
bool bfs(){
memset(d,-,sizeof(d));
queue<int>q;
q.push(S);
d[S] = ;
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].flow > && d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
return d[T] > -;
}
double dfs(int u,double low){
if(u == T) return low;
double tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow>&&d[e[i].to] == d[u]+ && (a=dfs(e[i].to,min(e[i].flow,low))) > ){
e[i].flow -= a;
e[i^].flow += a;
low -= a;
tmp += a;
if(low <= ) break;
}
}
if(tmp <= ) d[u] = -;
return tmp;
}
bool dinic(){
double flow = m;
while(bfs()){
memcpy(cur,head,sizeof(head));
double tmp = dfs(S,INF);
if(tmp > ) flow -= tmp;
}
return flow <= ;
}
void build(double delta){
memset(head,-,sizeof(head));
tot = ;
for(int i = ; i <= m; ++i){
add(S,i+n,1.0);
add(i+n,x[i],INF);
add(i+n,y[i],INF);
}
for(int i = ; i <= n; ++i) add(i,T,delta);
}
int main() {
while(~scanf("%d %d",&n,&m)){
S = ;
T = n + m + ;
for(int i = ; i <= m; ++i)
scanf("%d %d",x+i,y+i);
if(m == ) printf("1\n1\n");
else{
double low = ,high = 1.0*m,mid;
const double exps = 1.0/(n*n);
while(high - low >= exps){
mid = (low + high)/2.0;
build(mid);
if(dinic()) high = mid;
else low = mid;
}
build(low);
dinic();
int cnt = ,ans[maxn];
for(int i = ; i <= n; ++i)
if(d[i] > ) ans[cnt++] = i;
printf("%d\n",cnt);
for(int i = ; i < cnt; ++i)
printf("%d%c",ans[i],'\n');
}
}
return ;
}
第二种建模方案
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,next;
double flow;
arc(int x = ,double y = ,int z = -){
to = x;
flow = y;
next = z;
}
};
arc e[maxn*maxn];
int head[maxn],cur[maxn],d[maxn],du[maxn];
int tot,S,T,n,m,cnt;
pii p[maxn*maxn];
void add(int u,int v,double flow){
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
bool bfs(){
memset(d,-,sizeof(d));
d[S] = ;
queue<int>q;
q.push(S);
cnt = ;
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].flow > && d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q.push(e[i].to);
cnt++;
}
}
}
return d[T] > -;
}
double dfs(int u,double low){
if(u == T) return low;
double tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow > && d[u] + == d[e[i].to]&&(a=dfs(e[i].to,min(e[i].flow,low)))>){
e[i].flow -= a;
e[i^].flow += a;
low -= a;
tmp += a;
if(low <= ) break;
}
}
if(tmp <= ) d[u] = -;
return tmp;
}
bool dinic(){
double ans = n*m;
while(bfs()){
memcpy(cur,head,sizeof(head));
ans -= dfs(S,INF);
}
return ans/2.0 > ;
}
void build(double g){
memset(head,-,sizeof(head));
for(int i = tot = ; i < m; ++i){
add(p[i].first,p[i].second,);
add(p[i].second,p[i].first,);
}
for(int i = ; i <= n; ++i){
add(S,i,m);
add(i,T,m+g*2.0-du[i]);
}
}
int main() {
while(~scanf("%d %d",&n,&m)){
S = ;
T = n + ;
memset(du,,sizeof(du));
for(int i = ; i < m; ++i){
scanf("%d %d",&p[i].first,&p[i].second);
++du[p[i].first];
++du[p[i].second];
}
if(!m) printf("1\n1\n");
else{
const double exps = 1.0/(n*n);
double low = ,high = m;
while(high - low >= exps){
double mid = (low + high)/2.0;
build(mid);
if(dinic()) low = mid;
else high = mid;
}
build(low);
dinic();
printf("%d\n",cnt);
for(int i = ; i <= n; ++i)
if(d[i] > -) printf("%d\n",i);
}
}
return ;
}
POJ 3155 Hard Life的更多相关文章
- POJ 3155 Hard Life(最大密度子图+改进算法)
Hard Life Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 9012 Accepted: 2614 Case Ti ...
- POJ 3155 Hard Life 最大密度子图 最大权闭合图 网络流 二分
http://poj.org/problem?id=3155 最大密度子图和最大权闭合图性质很相近(大概可以这么说吧),一个是取最多的边一个是取最多有正贡献的点,而且都是有选一种必须选另一种的限制,一 ...
- POJ 3155 Hard Life(最大密度子图)
裸题.输入一个无向图,输出最大密度子图(输出子图结点数和升序编号). 看了<最小割模型在信息学竞赛中的应用——胡伯涛>的一部分,感觉01分数规划问题又是个大坑.暂时还看不懂. 参考http ...
- poj 3155 最大密度子图
思路: 这个还是看的胡伯涛的论文<最小割在信息学竞赛中的应用>.是将最大密度子图问题转化为了01分数规划和最小割问题. 直接上代码: #include <iostream> # ...
- poj 3155 二分+最小割求实型最小割(最大密集子图)
/* 最大密集子图子图裸题 解法:设源点s和汇点t 根据胡波涛的<最小割模型在信息学中的应用> s-每个点,权值为原边权和m, 每个点-t,权值为m+2*g-degree[i], 原来的边 ...
- POJ 3155:Hard Life(最大密度子图)
题目链接 题意 给出n个人,和m对有冲突的人.要裁掉一些人,使得冲突率最高,冲突率为存在的冲突数/人数. 思路 题意可以转化为,求出一些边,使得|E|/|V|最大,这种分数规划叫做最大密度子图. 学习 ...
- [转] POJ图论入门
最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意: ...
- poj很好很有层次感(转)
OJ上的一些水题(可用来练手和增加自信) (POJ 3299,POJ 2159,POJ 2739,POJ 1083,POJ 2262,POJ 1503,POJ 3006,POJ 2255,POJ 30 ...
- POJ题目分类推荐 (很好很有层次感)
著名题单,最初来源不详.直接来源:http://blog.csdn.net/a1dark/article/details/11714009 OJ上的一些水题(可用来练手和增加自信) (POJ 3299 ...
随机推荐
- 如何让Jboss的debug在myeclise上运行
1.在windows下运行jboss的debug.bat 看见监听的端口 2.打开myeclipse 点击选择 ①你要配置的名字(随意) ②myeclipse中选中该项目 ③jboss的启动的ip地址 ...
- idea 编辑器 光标问题!(insert键)
今天写代码不小心按了键盘的insert键,光标莫名闪退了 ,重新打开的时候发现 光标变成了 按了insert 的效果 ,简直无语的要命啊! 这敲代码太恶心了!怒搜资料 结果找到了解决办法! 1.打 ...
- Java7的那些新特性
本文介绍的java 7新特性很多其它的感觉像是语法糖.毕竟java本身已经比較完好了.不完好的非常多比較难实现或者是依赖于某些底层(比如操作系统)的功能. 不过java7也实现了类似aio的强大功能. ...
- 12:打印 1 到最大的 n 位数
题目:输入数字 n.按顺序打印出从 1 到 最大的 n 位十进制数.比方输入 3 ,则打印出 1.2 .3 一直到最大的3位数即 999. 解析: easy知道不能用 int 等数字类型表示(大数问题 ...
- pcapy-0.10.8 安装
(1)下载 http://corelabs.coresecurity.com/index.php?module=Wiki&action=view&type=tool&name= ...
- pgsql数据库备份还原记
今天又搞了一个pgsql 的备份还原,差一点没有成功,以前总是想当然的用,没认真想背后的东西,也没对过程中的疑问做记录,所以后面也没什么印象,常见常新,这次既然又遇到就总结一下. 之前操作pgsql数 ...
- 记录一下c++的一点指针所得
c++有两种传值可以改变外部参数,一种是传递指针,另一种是传递引用,对于前者,可以用Ugly(but explicitly),对于后者,Clean(but hidden),在传递的时候有一种值得注意的 ...
- [poj 3349] Snowflake Snow Snowflakes 解题报告 (hash表)
题目链接:http://poj.org/problem?id=3349 Description You may have heard that no two snowflakes are alike. ...
- Linux系统安装Redis数据库
Redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorte ...
- MySQL表不能修改、删除等操作,卡死、锁死情况的处理办法。
MySQL如果频繁的修改一个表的数据,那么这么表会被锁死.造成假死现象. 比如用Navicat等连接工具操作,Navicat会直接未响应,只能强制关闭软件,但是重启后依然无效. 解决办法: 首先执行: ...