题意:略

怎样判断属于S,T集合。

如果从S出发到不了某点,该点出发也到不了T,那么割给那边都行。

如果S出发能到该点,该点出发也能到T,这种情况下dinic没结束。

只能从S到该点:只能分到S集。只能从该点到T,T集。

这题中两种都能分到时,假如S表示0,那贪心分到S。这样只要看它能不能到T,如果能到T,一定要选1,否则就选0.用类似SPFA的操作。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
#define mkp make_pair
using namespace std;
const double EPS=1e-;
typedef long long lon;
const lon SZ=,SSZ=,APB=,one=,INF=0x7FFFFFFF,mod=;
int n,m,src[SZ][SZ],knum,arr[SZ],S=,T=;
int ans[SZ],mp[SZ][SZ],dep[SZ];
bool vst[SZ]; void init()
{
cin>>n>>m;
for(int i=;i<=m;++i)
{
int a,b;
cin>>a>>b;
src[a][b]=src[b][a]=;
}
cin>>knum;
for(int i=;i<=knum;++i)
{
int a,b;
cin>>a>>b;
ans[a]=arr[a]=b;
vst[a]=;
}
} void add(int u,int v,int w)
{
mp[u][v]=w;
} bool bfs()
{
memset(dep,,sizeof(dep));
dep[S]=;
queue<int> q;
q.push(S);
for(;q.size();)
{
int fr=q.front();
q.pop();
for(int i=;i<=T;++i)
{
if(!dep[i]&&mp[fr][i])
{
dep[i]=dep[fr]+;
q.push(i);
if(i==T)return ;
}
}
}
return ;
} int dinic(int x,int flow)
{
if(x==T)return flow;
else
{
int rem=flow;
for(int i=;i<=T&&rem;++i)
{
if(dep[i]==dep[x]+&&mp[x][i])
{
int tmp=dinic(i,min(rem,mp[x][i]));
if(!tmp)dep[i]=;
rem-=tmp;
mp[x][i]-=tmp,mp[i][x]+=tmp;
}
}
return flow-rem;
}
} void build(int x)
{
memcpy(mp,src,sizeof(src));
for(int i=;i<=n;++i)
{
if(vst[i])
{
if(arr[i]&(<<x))
{
add(i,T,INF);
add(T,i,);
}
else
{
add(S,i,INF);
add(i,S,);
}
}
else
{
// add(S,i,APB);
// add(i,S,0);
// add(i,T,APB);
// add(T,i,0);
}
}
} int v2[SZ]; void calc(int x)
{
memset(v2,,sizeof(v2));
queue<int> q;
for(int i=;i<=n;++i)
{
if(mp[i][T])v2[i]=,q.push(i);
}
for(;q.size();)
{
int fr=q.front();
q.pop();
for(int i=;i<=n;++i)
{
if(!v2[i]&&mp[i][fr])
{
v2[i]=;
q.push(i);
}
}
}
for(int i=;i<=n;++i)
{
//if(x==0)cout<<v2[i]<<endl;
if(v2[i]&&!vst[i])ans[i]|=<<x;
}
} void work()
{
for(int i=;i<;++i)
{
build(i);
int res=;
for(;bfs();)res+=dinic(S,INF);
//cout<<"res: "<<res<<endl;
calc(i);
}
for(int i=;i<=n;++i)
{
cout<<ans[i]<<endl;
}
} void release()
{
memset(vst,,sizeof(vst));
memset(src,,sizeof(src));
memset(ans,,sizeof(ans));
} int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
//cout<<(1<<31)<<endl;
int casenum;
cin>>casenum;
//cout<<casenum<<endl;
for(int time=;time<=casenum;++time)
//for(int time=1;scanf(" %s",ch+1)!=EOF;++time)
{
init();
work();
release();
}
return ;
}

spoj839Optimal Marks的更多相关文章

  1. 【BZOJ-2400】Spoj839Optimal Marks 最小割 + DFS

    2400: Spoj 839 Optimal Marks Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 567  Solved: 202[Submit ...

  2. 贪心 Codeforces Round #301 (Div. 2) B. School Marks

    题目传送门 /* 贪心:首先要注意,y是中位数的要求:先把其他的都设置为1,那么最多有(n-1)/2个比y小的,cnt记录比y小的个数 num1是输出的1的个数,numy是除此之外的数都为y,此时的n ...

  3. 839. Optimal Marks - SPOJ

    You are given an undirected graph G(V, E). Each vertex has a mark which is an integer from the range ...

  4. (CodeForces )540B School Marks 贪心 (中位数)

    Little Vova studies programming to p. Vova is very smart and he can write every test for any mark, b ...

  5. 图论(网络流):SPOJ OPTM - Optimal Marks

    OPTM - Optimal Marks You are given an undirected graph G(V, E). Each vertex has a mark which is an i ...

  6. CodeForces 540B School Marks(思维)

    B. School Marks time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  7. 【SPOJ839】Optimal Marks 网络流

    You are given an undirected graph G(V, E). Each vertex has a mark which is an integer from the range ...

  8. The table name must be enclosed in double quotation marks or sqare bracket while accessing EXCEL by

      1  Preface DB Query Analyzer is presented by Master Gen feng, Ma from Chinese Mainland. It has Eng ...

  9. Codeforces831C Jury Marks

    C. Jury Marks time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

随机推荐

  1. CF830A Office Keys(贪心)

    CF830A Office Keys [题目链接]CF830A Office Keys [题目类型]贪心 &题意: 有n个人,k个钥匙,一个目的地,求让n个人都回到目的地的最短时间,每个人都要 ...

  2. List、Set、Map集合

    1 List接口 我们掌握了Collection接口的使用后,再来看看Collection接口中的子类,他们都具备那些特性呢? 接下来,我们一起学习Collection中的常用几个子类(List集合. ...

  3. 计算机网络网络层的IP地址划分及子码

    现在在网络层,即就是TCP/IP协议里的网际互联层,最流行IP协议的就是IPV4.其中IP地址的格式是由32位二进制数字表示的,通常为了人们阅读习惯,将其转换成点分十进制来表示,如:192.168.1 ...

  4. 搭建apache本地服务器·Win

    1.下载apache地址:https://www.apachelounge.com/download/ 注意:下载压缩包如下 httpd-2.4.37-win64-VC15.zip 其中根据自己电脑的 ...

  5. Python之io概念

    """ 同步,异步: 强调结果,调用者最终是否得到想要的结构 阻塞非阻塞: 强调时间是否等待 io二个阶段 1.数据准备阶段 2.内核空间复制回用户空间缓冲区阶段 发生i ...

  6. 4步解决“Microsoft Office Professional Plus 2013在安装过程中出错”

    公司新搭建了AD域,公司内使用了1年多的电脑,现在要加入域,加域过程问题错综复杂. 其中一台电脑上,反应说Excel经常卡住,严重影响使用,所以考虑重装office2013.在控制面板卸载了: 卸载完 ...

  7. IT题库7-线程加锁

    转载:http://www.cnblogs.com/linjiqin/p/3208843.html 一.同步问题提出 线程的同步是为了防止多个线程访问一个数据对象时,对数据造成的破坏.例如:两个线程T ...

  8. Spring Cloud 和 Dubbo 比较

    Spring Cloud 和 Dubbo 比较

  9. 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'groups)VALUES('1','hh','hh@163.com','Boss')' at line 1

    mysql8.0版本 在已存在的表里插入一条数据 insert INTO api_user(id,username,email,groups)VALUES('1','hh','hh@163.com', ...

  10. QT4.8应用控制程序设计

    2012-02-20 22:06:59 从uboot到kernel最新版最后到QT最新版移植都完成后,从初级阶段要走向中级阶段了.初步一个设想是然2440通过QT界面控制小车.先做一个界面模块控制LE ...