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 ...
随机推荐
- 亲历:IT 从业者避免猝死攻略 v1.0
作者:香蕉痞 出处:http://www.geekpark.net/read/view/191188?u=0 亲历:IT 从业者避免猝死攻略 v1.0 By 香蕉痞 | 2013/10/28 [核心提 ...
- debian 9 安装无线网卡
#添加源 echo "deb http://httpredir.debian.org/debian/ stretch main contrib non-free" >> ...
- python和sudo python 出现no module named XXX
今天使用python执行文件,然而显示权限不够, 但是加上sudo之后,又会显示no module named XXX 问题在于两者python路径中包含的库不同.解决方法: 分别进入两个python ...
- JS中Math函数基础
Math 是数学函数,但又属于对象数据类型 typeof Math => ‘object’
- Maven学习总结(23)——Maven常用命令介绍
1.生成eclipse项目:mvn eclipse:eclipse 2.清除eclipse的一些系统设置:mvn eclipse:clean 3.mvn tomcat:run 在tomcat里面运行 ...
- javascript-datatable错误提示
datatables插件在使用的时候出现了如下错误提示**出现此错误的原因可能是你写的table中没有加上<thead>和<tbody>标签所致** 来自为知笔记(Wi ...
- 数据库-mongodb-聚合与map reduce
分组统计:group() 简单聚合:aggregate() 强大统计:mapReduce() Group函数: 1.不支持集群.分片,无法分布式计算 2.需要手写聚合函数的业务逻辑 curr指当前行, ...
- 极路由4pro交叉编译c、c++的代码
首先在官方文档中下载SDK 极路由4pro是mtmips架构,即32位mips,小端的,因此下载这个 防止官方的下载链接失效,特意上传到百度网盘: 链接:https://pan.baidu.com/s ...
- Scratch单机版下载
Scratch单机版下载 这两个地址速度比较快: Adobe Air:http://7dx.pc6.com/wwb5/AdobeAIR2800127.zip Scratch :http://7dx.p ...
- cocos2d-x-3.3rc2-003 cocos中的引用计数和自己主动释放池
点击打开链接