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 扫 ...
随机推荐
- 用C#.NET调用Java开发的WebService传递int,double问题,出现java无法获得值!
https://www.cnblogs.com/zhbsh/archive/2013/04/22/3035477.html 用C#.NET调用Java开发的WebService时,先在客户端封装的带有 ...
- P1829 [国家集训队]Crash的数字表格 / JZPTAB
推式子太快乐啦!虽然我好蠢而且dummy和maomao好巨(划掉) 思路 莫比乌斯反演的题目 首先这题有\(O(\sqrt n)\)的做法但是我没写咕咕咕 然后就是爆推一波式子 \[ \sum_{i= ...
- 深度学习课程笔记(四)Gradient Descent 梯度下降算法
深度学习课程笔记(四)Gradient Descent 梯度下降算法 2017.10.06 材料来自:http://speech.ee.ntu.edu.tw/~tlkagk/courses_MLDS1 ...
- 【C#】Using的一个比较好的语言文字解释
其实很早就开始使用using了.但是对这个语法糖我自己一直没有总结也没有一个很好的文字描述解释.今天看其他的博文的时候发现有人对其做了简单的解释我觉得很好,很适合一种讲解.于是抄录下来 using ( ...
- sublime text 中文显示异常
本文链接:sublime text 中文显示异常 http://www.cnblogs.com/daysme/p/7549857.html 前言 sublime text 3143 用了一年时候,最近 ...
- Oracle DBHelper 第二版
public static class OracleDBHelper { public static OracleCommand cmd = null; public static OracleCon ...
- 【MySQL】【一】shell
进入MySQL:mysql -u root -p 查看当前数据库下所有库:mysql> show databases; 切换到某库:mysql> use sys; 查看sys库下所有表:m ...
- Pandas 基础(1) - 初识及安装 yupyter
Hello, 大家好, 昨天说了我会再更新一个关于 Pandas 基础知识的教程, 这里就是啦......Pandas 被广泛应用于数据分析领域, 是一个很好的分析工具, 也是我们后面学习 machi ...
- curl java 模拟http请求
curl java 模拟http请求 直接上代码: public static void main(String args[]) throws Exception { String url = &qu ...
- git 修改文件夹名字后如何提交
将文件夹名字从 v1.0.1 修改为 v1.0.2 git add --ignore-removal "v1.0.2/xsxsx"