2015-2016 ACM-ICPC, NEERC, Moscow Subregional Contest J - Jealousy

时间:2022-09-29 08:34:10

题意:有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的更多相关文章

  1. 2016 NEERC&comma; Moscow Subregional Contest K&period; Knights of the Old Republic(Kruskal思想)

    2016 NEERC, Moscow Subregional Contest K. Knights of the Old Republic 题意:有一张图,第i个点被占领需要ai个兵,而每个兵传送至该 ...

  2. 2018-2019 ICPC&comma; NEERC&comma; Southern Subregional Contest

    目录 2018-2019 ICPC, NEERC, Southern Subregional Contest (Codeforces 1070) A.Find a Number(BFS) C.Clou ...

  3. Codeforces 2018-2019 ICPC&comma; NEERC&comma; Southern Subregional Contest

    2018-2019 ICPC, NEERC, Southern Subregional Contest 闲谈: 被操哥和男神带飞的一场ACM,第一把做了这么多题,荣幸成为7题队,虽然比赛的时候频频出锅 ...

  4. 2018-2019 ICPC&comma; NEERC&comma; Southern Subregional Contest &lpar;Online Mirror&rpar; Solution

    从这里开始 题目列表 瞎扯 Problem A Find a Number Problem B Berkomnadzor Problem C Cloud Computing Problem D Gar ...

  5. 2010-2011 ACM-ICPC&comma; NEERC&comma; Moscow Subregional Contest Problem D&period; Distance 迪杰斯特拉

    Problem D. Distance 题目连接: http://codeforces.com/gym/100714 Description In a large city a cellular ne ...

  6. 2010-2011 ACM-ICPC&comma; NEERC&comma; Moscow Subregional Contest Problem C&period; Contest 水题

    Problem C. Contest 题目连接: http://codeforces.com/gym/100714 Description The second round of the annual ...

  7. 2016-2017 ACM-ICPC&comma; NEERC&comma; Moscow Subregional Contest Problem L&period; Lazy Coordinator

    题目来源:http://codeforces.com/group/aUVPeyEnI2/contest/229511 时间限制:1s 空间限制:512MB 题目大意: 给定一个n 随后跟着2n行输入 ...

  8. Codeforces1070 2018-2019 ICPC&comma; NEERC&comma; Southern Subregional Contest &lpar;Online Mirror&comma; ACM-ICPC Rules&comma; Teams Preferred&rpar;总结

    第一次打ACM比赛,和yyf两个人一起搞事情 感觉被两个学长队暴打的好惨啊 然后我一直做*题,yyf一直在切神仙题 然后放一波题解(部分) A. Find a Number LINK 题目大意 给你 ...

  9. codeforce1070 2018-2019 ICPC&comma; NEERC&comma; Southern Subregional Contest &lpar;Online Mirror&comma; ACM-ICPC Rules&comma; Teams Preferred&rpar; 题解

    秉承ACM团队合作的思想懒,这篇blog只有部分题解,剩余的请前往星感大神Star_Feel的blog食用(表示男神汉克斯更懒不屑于写我们分别代写了下...) C. Cloud Computing 扫 ...

随机推荐

  1. Sentiment Analysis resources

    Wikipedia: Sentiment analysis (also known as opinion mining) refers to the use of natural language p ...

  2. form作为module name 悲剧了

    爆出很无语的错误,也怪我,没有实地的debug. 所以,module name应该是不能碰关键词类似,最好custom一点好.

  3. Android(java)学习笔记141:各种边距设置

    1. android:layout_paddingLeft 内边距,对谁用,指的是谁的内部内容边距 2. android:layout_marginLeft 外边距,对谁用,指的是谁距离外层容器的边距 ...

  4. Yii 打造带有缓存功能的AR

    继承AR类 重写 findByPk方法为pk  还有afterSave afterDelete 通过对象主键缓存其属性  在insert update delete 操作时候 都会自动更新缓存还是挺方 ...

  5. 更改mysql数据库latin1&lowbar;swedish&lowbar;ci为utf8

    原文在http://bingu.net/472/latin1_swedish_ci-to-utf8_general_ci/把下列文件保存为一个.php文件,然后运行 <?phpdefine('D ...

  6. 开源一个适用iOS的数据库表结构更新机制的代码

    将前段时间开源的代码.公布一下: ARDBConfig On the iOS, provide a database table structure update mechanism, ensure ...

  7. Django&plus;MySQL开发项目:内容管理系统cms(一)

    Baker-Miller Pink被科学方法证实可以平静情绪并且抑制食欲的颜色,具有amazing的效果.基百里面说实验结果表明该颜色具有: "a marked effect on lowe ...

  8. RadioButton的图标改变大小(TextView也适用)

    RadioButton的图标大小并没有相应的布局参数,本文通过自定义属性的方式自定义RadioButton,实现控制图片大小. 本文要点: 自定义属性的使用. 解决RadioButton文字上.下.左 ...

  9. React Native 从入门到原理一

    React Native 从入门到原理一 React Native 是最近非常火的一个话题,介绍如何利用 React Native 进行开发的文章和书籍多如牛毛,但面向入门水平并介绍它工作原理的文章却 ...

  10. Oracle中rownum和rowid的理解

    rownum,rowid都叫伪列. 但是,rownum是逻辑上的编号,且其值总是从1开始,每行的rounum不是固定的.而rowid是“物理”编号.若数据库文件没有移动,则每行的 rowid一般是固定 ...