2015-2016 ACM-ICPC, NEERC, Moscow Subregional Contest J - Jealousy
题意:有n张照片,每张照片上有一些妹子,要按照片顺序给妹纸安排男朋友,如果妹纸i安排的男朋友之前有女朋友,那么费用+wi,求总费用最小,和输出路径
题解:费用流,先把照片天数建点i连i+1,流量k(最多的男朋友数量),费用0,再把所有按照片顺序出现的妹纸拆点,自己流自己,流量1,费用-inf(保证要安排上),假设当前妹纸是第i天出现的,如果该妹纸之前出现过,我们连一条上一次的到当前点,流量为1,费用0(表示沿用上一次的男朋友),否则从第1个照片点连一条流量为1,费用0的边(表示安排一个之前没有用过的男朋友),还要从i连一条流量为1,费用wi的边(表示用上一天某个妹纸的男朋友),连一条到i+1流量为1,费用0的边(为了让下一天的妹纸能重复利用男朋友),输出路径时dfsk次表示经过的点都是安排了第i号朋友
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
//#define cd complex<double>
#define ull unsigned long long
//#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
using namespace std;
const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=20200+10,maxn=200000+10,inf=0x3f3f3f3f;
struct edge{
int from,to,Next,c;
int cost;
}e[maxn];
int cnt,head[N],s,t,pre[N],path[N];
ll dis[N];
bool vis[N];
void init(){memset(head,-1,sizeof head);cnt=0;}
void add(int u,int v,int c,int cost)
{
e[cnt].from=u;e[cnt].to=v;e[cnt].c=c;e[cnt].cost=cost;e[cnt].Next=head[u];head[u]=cnt++;
e[cnt].from=v;e[cnt].to=u;e[cnt].c=0;e[cnt].cost=-cost;e[cnt].Next=head[v];head[v]=cnt++;
}
bool spfa()
{
memset(pre,-1,sizeof pre);
memset(dis,INF,sizeof dis);
memset(vis,0,sizeof vis);
dis[s]=0;vis[s]=1;
queue<int>q;q.push(s);
while(!q.empty())
{
int x=q.front();q.pop();
vis[x]=0;
for(int i=head[x];~i;i=e[i].Next)
{
int te=e[i].to;
if(e[i].c>0&&dis[x]+e[i].cost<dis[te])
{
dis[te]=dis[x]+e[i].cost;
pre[te]=x;path[te]=i;
if(!vis[te])q.push(te),vis[te]=1;
}
}
}
return pre[t]!=-1;
}
ll mincostmaxflow()
{
ll cost=0,flow=0;
while(spfa())
{
int f=inf;
for(int i=t;i!=s;i=pre[i])if(e[path[i]].c<f)f=e[path[i]].c;
flow+=f;cost+=dis[t]*f;
for(int i=t;i!=s;i=pre[i])
e[path[i]].c-=f,e[path[i]^1].c+=f;
}
return cost;
}
int v[N],has[N],l[N],r[N],tot,ans[N];
void dfs(int u,int p)
{
ans[u]=p;if(u==t)return ;
for(int i=head[u];~i;i=e[i].Next)
{
if(i&1)continue;
if(e[i^1].c==0)continue;
e[i^1].c--,e[i].c++;
dfs(e[i].to,p);
return ;
}
}
int main()
{
int n,m,k;
scanf("%d%d%d",&n,&k,&m);
init();s=0,t=n+1;
for(int i=1;i<=n+1;i++)add(i-1,i,k,0);
for(int i=1;i<=m;i++)scanf("%d",&v[i]);
tot=n+1;ll sum=0;
for(int x,y,i=1;i<=n;i++)
{
for(l[i]=tot+1,scanf("%d",&x);x;x--)
{
scanf("%d",&y);tot++;
if(has[y])add(has[y],tot,1,0);
else add(1,tot,1,0);
add(i,tot,1,v[y]);add(tot,tot+1,1,-inf);add(tot+1,i+1,1,0);
tot++;has[y]=tot;sum+=inf;
}
r[i]=tot;
}
printf("%lld\n",mincostmaxflow()+sum);
for(int i=1;i<=k;i++)dfs(0,i);
for(int i=1;i<=n;i++)
{
for(int j=l[i];j<=r[i];j+=2)
printf("%d ",ans[j]);
puts("");
}
return 0;
}
/********************
********************/
2015-2016 ACM-ICPC, NEERC, Moscow Subregional Contest J - Jealousy的更多相关文章
- 2016 NEERC, Moscow Subregional Contest K. Knights of the Old Republic(Kruskal思想)
2016 NEERC, Moscow Subregional Contest K. Knights of the Old Republic 题意:有一张图,第i个点被占领需要ai个兵,而每个兵传送至该 ...
- 2018-2019 ICPC, NEERC, Southern Subregional Contest
目录 2018-2019 ICPC, NEERC, Southern Subregional Contest (Codeforces 1070) A.Find a Number(BFS) C.Clou ...
- Codeforces 2018-2019 ICPC, NEERC, Southern Subregional Contest
2018-2019 ICPC, NEERC, Southern Subregional Contest 闲谈: 被操哥和男神带飞的一场ACM,第一把做了这么多题,荣幸成为7题队,虽然比赛的时候频频出锅 ...
- 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror) Solution
从这里开始 题目列表 瞎扯 Problem A Find a Number Problem B Berkomnadzor Problem C Cloud Computing Problem D Gar ...
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem D. Distance 迪杰斯特拉
Problem D. Distance 题目连接: http://codeforces.com/gym/100714 Description In a large city a cellular ne ...
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem C. Contest 水题
Problem C. Contest 题目连接: http://codeforces.com/gym/100714 Description The second round of the annual ...
- 2016-2017 ACM-ICPC, NEERC, Moscow Subregional Contest Problem L. Lazy Coordinator
题目来源:http://codeforces.com/group/aUVPeyEnI2/contest/229511 时间限制:1s 空间限制:512MB 题目大意: 给定一个n 随后跟着2n行输入 ...
- Codeforces1070 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)总结
第一次打ACM比赛,和yyf两个人一起搞事情 感觉被两个学长队暴打的好惨啊 然后我一直做傻子题,yyf一直在切神仙题 然后放一波题解(部分) A. Find a Number LINK 题目大意 给你 ...
- codeforce1070 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred) 题解
秉承ACM团队合作的思想懒,这篇blog只有部分题解,剩余的请前往星感大神Star_Feel的blog食用(表示男神汉克斯更懒不屑于写我们分别代写了下...) C. Cloud Computing 扫 ...
随机推荐
- Custom Quality Profiles in SonarQube
https://medium.com/ltunes/custom-quality-profiles-in-sonarqube-part-1-8754348b9369 Creating Custom Q ...
- ProgrammingError: You must not use 8-bit bytestrings...
问题出现: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit byte ...
- 搭建git 服务器
Gogs 什么是 Gogs? Gogs 是一款极易搭建的自助 Git 服务. https://gogs.io/docs
- 常用处理数组、字符串API → forEach every some sort map filter slice split indexOf concat substring substr splice join toString replace
Object与Array的语法糖 var arr = [1,2,3]; // [] 是 new Array(1,2,3) 的语法糖(简写) var obj = {'name':2,'age':3}; ...
- JZ2440存储管理器--SDRAM
为了cpu访问外部设备,ARM提供一个存储管理器部件,提供访问外部设备的所需的信号(对SDRAM.网卡.nor等设备进行初始化,以便存储器管理器配合CPU进行与外设数据通讯). CPU通常读写一 ...
- 如何规避Adobe Flash Player中重橙网络的广告弹窗
具体解决之道,参见卡饭论坛风之咩的帖子:https://bbs.kafan.cn/thread-2123485-1-1.html
- 【JS】JavaScript中innerHTML与innerText,createTextNode的区别
innerHTML和innerText 它们都会把元素内内容替换掉,区别在于: innerHTML 会把替换内容里的 HTML 标记解释执行. innerText 会把替换内容里的 HTML 标记原样 ...
- 20165306 实验二 Java面向对象程序设计
实验二 Java面向对象程序设计 实验要求 1.提交最后三个JUnit测试用例(正常情况,错误情况,边界情况)都通过的截图,截图上要有画图加水印,输入自己的学号.本提交点考查JUnit会不会使用,测试 ...
- 每日质量NPM包事件绑定_bindme(详解React的this)
一.bindme 官方定义: is a helper to bind a list of methods to an object reference 理解: 因为不推荐在render()里构建函数, ...
- CentOS7 下curl使用
curl工具工具的主页:https://curl.haxx.se/NAMEcurl - transfer a URL SYNOPSIScurl [options] [URL...] DESCRIPTI ...