题意:略

怎样判断属于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. 551.学生出勤记录I

    /* * @lc app=leetcode.cn id=551 lang=java * * [551] 学生出勤记录 I * * https://leetcode-cn.com/problems/st ...

  2. Centos7 pcs pacemaker高可用安装配置详解

  3. mysql数据库优化之索引的维护和优化

    这里是一个工具,即pt-duplicate-key-checker工具 用来检查重复及冗余的索引 用法如下:pt-duplicate-key-checker  -uroot  -p密码  -h127. ...

  4. mtd-utils交叉编译安装

    一.获取源码并解压 存储于/home/zhangyi/work/psoc_ltp/tools-ltp-ddt中,解压后的源码存于上一层目录. 1.mtd-utils-2.0.0 wget ftp:// ...

  5. Linux error numbers

    Linux error numbers, straight from the horse's mouth. #define EPERM 1 /* Operation not permitted */ ...

  6. 【题解】Luogu P1344 [USACO4.4]追查坏牛奶Pollutant Control

    原题传送门 看到这种题,应该一眼就能知道考的是最小割 没错这题就是如此简单,跑两遍最大流(最小割=最大流),一次边权为题目所给,一次边权为1 还有一种优化,优化后只需跑一次最大流,把每条边的权值改成w ...

  7. AHOI2019游记

    day0 早上八点钟出发,下午一点左右到了(高速好堵啊) 下午试机,打了一发线段树,堆和其他一些东西,测试完没有问题就走了 惊奇发现旁边竟然是合肥市队的WC银牌hzy,%%% 晚上%了一发金牌爷yg就 ...

  8. 直方图均衡化与Matlab代码实现

    昨天说了,今天要好好的来解释说明一下直方图均衡化.并且通过不调用histeq函数来实现直方图的均衡化. 一.直方图均衡化概述 直方图均衡化(Histogram Equalization) 又称直方图平 ...

  9. 完整的多文件上传实例(java版)

    昨天刚刚做了一个文件列表上传,后端很简单,用 MultipartFile[] files 获取文件流数组,后端就当IO流操作就可以,似乎好像没啥好写的,但是!!!!!前端是真的糙单.要是自己写一个前端 ...

  10. SpringBoot Tomcat启动报错

    中间的桥梁就是下面这个依赖 <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over ...