题意:有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, Moscow Subregional Contest K. Knights of the Old Republic(Kruskal思想)

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

  2. 2018-2019 ICPC, NEERC, Southern Subregional Contest

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

  3. Codeforces 2018-2019 ICPC, NEERC, Southern Subregional Contest

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

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 2016-2017 ACM-ICPC, NEERC, Moscow Subregional Contest Problem L. Lazy Coordinator

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

  8. Codeforces1070 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)总结

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

  9. codeforce1070 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred) 题解

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

随机推荐

  1. 【修改密码】Linux下修改Mysql的用户(root)的密码

    修改的用户都以root为列.一.拥有原来的myql的root的密码: 方法一:在mysql系统外,使用mysqladmin# mysqladmin -u root -p password " ...

  2. POJ 2409 Let it Bead

    思路 同这道题,只是颜色数从3变成c 代码 #include <cstdio> #include <algorithm> #include <cstring> #d ...

  3. 【论文笔记】Zero-shot Recognition via semantic embeddings and knowledege graphs

    Zero-shot Recognition via semantic embeddings and knowledege graphs   2018-03-31  15:38:39  [Abstrac ...

  4. Machine.config与web.config

    应用范围的不同 结点介绍 原理介绍 异常 web.config详解

  5. Latex: 使 tabular 居中

    参考: How to center the table in Latex Latex: 使 tabular 居中 解决方法1: { \centering \begin{tabular} ... \en ...

  6. Latex: extra alignment tab has been changed to cr

    参考: Error: extra alignment tab has been changed to \cr Latex: extra alignment tab has been changed t ...

  7. layui upload 后台获取不到值

    后台获取不到值方法一: <script> layui.use('upload', function () { var upload = layui.upload; //执行实例 var u ...

  8. _faction

    一.自定义阵营独立于联盟,部落,联盟和部落玩家可以加入同一阵营 二._function_menu表可以配置自定义阵营开启 二.配合_pvp表,可以实现区域的自定义阵营PVP 三.配合_req表fact ...

  9. _itemmod_extra_equipments

    双甲 可以控制获得属性的倍率,及是否可以取回物 `stat_muil`属性倍率(item_template中stat) `enchant_muil`附魔效果中的属性倍率(一些附魔会提升属性,可在些配置 ...

  10. IDEA @Autowired 出现红色下划线 报红

    例如: 解决方法: