题意:略

怎样判断属于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. LNMP平台搭建之一:nginx编译安装

    参考博客:https://www.cnblogs.com/zhang-shijie/p/5294162.html   jack.zhang 一.环境说明 系统环境:centos6.5 [root@lo ...

  2. shiro认证登录实现

    准备工作: 在web.xml中配置shiro核心过滤器 在spring配置文件中提供核心过滤器运行所需要的辅助bean对象,在对象内注入安全管理器 拦截认证 配置三个url 拦截除了登录页面以及认证a ...

  3. MQ消息队列配置

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  4. 安装mysql时出现应用程序无法正常启动(0xc000007b)

    在重装mysql数据库(解压版)遇到了无法正常启动(0xc000007b)问题解决方案 问题描述: 在cmd控制台,mysql的安装路径下使用mysql install命令出现以下无法正常启动(0xc ...

  5. ACM-ICPC 2018 南京赛区网络预赛(A, J)

    A  签到题 Alice, a student of grade 666, is thinking about an Olympian Math problem, but she feels so d ...

  6. CentOS7编译安装SVN(subversion1.9.7)

    参考连接0:http://www.programering.com/a/MDMzYDMwATg.html参考连接1:http://www.zsythink.net/archives/13180.系统信 ...

  7. react问题解决的一些方法

    原文链接: https://segmentfault.com/a/1190000007811296?utm_source=tuicool&utm_medium=referral 初学者对Rea ...

  8. python学习笔记之socket(第七天)

         参考文档:              1.金角大王博客:http://www.cnblogs.com/alex3714/articles/5227251.html               ...

  9. Class打包成jar

    Class打包成jar 现在我的文件夹的目录在: C:\Users\linsenq\Desktop\cglibjar 我要把位于这个目录下的所有文件夹以及这个文件夹下的.class文件打成jar包 第 ...

  10. php,js 对字符串按位异或运算加密解密

    异或的符号是^.按位异或运算, 对等长二进制模式按位或二进制数的每一位执行逻辑按位异或操作. 操作的结果是如果某位不同则该位为1, 否则该位为0. xor运算的逆运算是它本身,也就是说两次异或同一个数 ...