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 ...
随机推荐
- Extjs iconCls 的用法
如何在按钮中加icon: 1.在Extjs中 { xtype:'button', text:'学生档案', iconCls:'file', }, 2.在css中写: .file{ background ...
- 【Python 学习】通过while循环和for循环猜测年龄
Python中可以通过while和for循环来限制猜测年龄的次数 1. 在猜测年龄的小程序中,不加循环的代码是这样的: age_of_yu = 23 guess_age = int(input(&qu ...
- [terry笔记]python内置函数
总结一下内置函数,Build-in Function. 一.数学运算类 abs(x) 求绝对值 complex([real[, imag]]) 创建一个复数 divmod(a, b) 分别取商和余数注 ...
- SpringAOP之CGLIB字节码增强
SpringAOP的基础原理就是动态代理 有两种实现方式:1)jdk动态代理 2)cglib动态代理 jdk动态代理和cglib动态代理的区别在于: cglib没有接口(通过继承父类) 只有实现类. ...
- HDU 4314 Contest 2
可以知道,逃出的人中,最后一个应当是A+B最长的,这是很容易发现的.那么,最选逃出去的必定是A+B最短的.这符合最优. 于是,可以把各小矮人按A+B的和由大到小排序.定义DP[i][j]为i个人中逃出 ...
- 剑指offer面试题26-复杂链表的复制
题目: 请实现函数ComplexListNode* Clone(ComplexListNode* pHead).复制一个复杂链表. 在复杂链表中.每个节点除了一个m_pNext指针指向下一个节点外,另 ...
- cocos2d-x 3.0的坑有哪些
问题一:setup.py 之后, ANT文件夹为什么创建不成功? ANT文件夹要指定到bin以下,NDK和SDK则指定要根文件夹就可以 问题二:cocos run -p android 之后,执行应用 ...
- 关于sql中的with(nolock)
SQL Server 中的 NOLOCK 究竟是什么意思 一般用于此类语句中:select * from t with(NOLOCK) nolock是不加锁查询.能够读取被事务锁定的数据,也称为脏读. ...
- 黑马day15 文件上传&apche的工具包
1.肯定要导入apche的jar包 2.要使用的类的介绍.. 2.1DiskFileItemFactory public DiskFileItemFactory(int sizeThreshold, ...
- jni javah
如何通过javah生成jni头文件 1.javah的使用说明: -classpath 给出包含native接口的java类的.class文件路径 -d / –o 指定生成的头文件的,-d只给出文件 ...