Problem Description
MZL is an active girl who has her own country.
Her big country has N cities numbered from 1 to N.She has controled the country for so long and she only remebered that there was a big earthquake M years ago,which made all the roads between the cities destroyed and all the city became broken.She also remebered that exactly one of the following things happened every recent M years:
1.She rebuild some cities that are connected with X directly and indirectly.Notice that if a city was rebuilt that it will never be broken again.
2.There is a bidirectional road between city X and city Y built.
3.There is a earthquake happened and some roads were destroyed.
She forgot the exactly cities that were rebuilt,but she only knew that no more than K cities were rebuilt in one year.Now she only want to know the maximal number of cities that could be rebuilt.At the same time she want you to tell her the smallest lexicographically plan under the best answer.Notice that 8 2 1 is smaller than 10 0 1.
Input
The first contains one integer T(T<=50),indicating the number of tests.
For each test,the first line contains three integers N,M,K(N<=200,M<=500,K<=200),indicating the number of MZL’s country ,the years happened a big earthquake and the limit of the rebuild.Next M lines,each line contains a operation,and the format is “1 x” , “2 x y”,or a operation of type 3.
If it’s type 3,first it is a interger p,indicating the number of the destoyed roads,next 2*p numbers,describing the p destoyed roads as (x,y).It’s guaranteed in any time there is no more than 1 road between every two cities and the road destoyed must exist in that time.
Output
The First line Ans is the maximal number of the city rebuilt,the second line is a array of length of tot describing the plan you give(tot is the number of the operation of type 1).
Sample Input
1
5 6 2
2 1 2
2 1 3
1 1
1 2
3 1 1 2
1 2
Sample Output
3
0 2 1
Hint
No city was rebuilt in the third year,city 1 and city 3 were rebuilt in the fourth year,and city 2 was rebuilt in the sixth year.
来自:https://blog.csdn.net/qwe2434127/article/details/47303263
这道题的建图方式足够只能让我膜拜大佬。
其实题目中要求的求重建的城市的数量最多,且重建城市的数量形成的序列的字典序最小,这个条件就有向费用流靠拢的意思,剩下建图就不停的把题意抽象的靠拢到费用流的模型上去。关于图论,我还得学习很多。
题意:有N个城市,一开始都处于被地震摧毁的状态。之后有M个询问,1 x表示可以将与x相连(包括x)的城市重建(重建后不再被地震破坏),每次能重建的城市的数量小于等于k;2 x y表示在x和y之间建立一条道路相同;3 p x1 y1...xp yp表示有p组道路被破坏,分别是x1 y1...xp yp;现要求每次询问1 x时要重建几座城市,使得所有最后重建的城市的数量最多,且重建城市的数量形成的序列的字典序最小。
思路:(费用流)
大致的建图方式:将所有城市与源点s相连,边的容量为1(为什么为1?因为城市只能被重建一次,建好后便不会被摧毁),费用为0。将所有1 x的操作弄成节点与汇点t相连,容量为k(因为每次最多只能重建k个城市),费用为0。并且每次1 x的操作中,将该节点和x联通的节点都连一条边,边的容量为无穷大,边的费用是cost(这个等下稍微要有点技巧),就相当于每次的1 x的操作要重建几座城市。
如果题目要求是的最多建立几座城市,那么最大流就够了,但是这里要用到最小费用最大流来实现字典序最小。假设cost一开始是一个较大的值,每次操作1 x之后这个cost就自减一个值,这样做的目的是让流量优先通过后面操作的1 x,来实现字典序的最小。要输出答案的话,只要遍历1 x的节点,将通向汇点的边的流量记录下来输出就是最小字典序
代码:
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <queue>
#include <cmath>
#include <time.h>
#include <vector>
#include <string>
#include <math.h>
#include <bitset>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define PI acos(-1.0)
#define ll long long
#define inf 0x3f3f3f3f
#define ull unsigned long long
using namespace std;
//来自:https://blog.csdn.net/qwe2434127/article/details/47303263
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,b) for(int i = a;i <= b;i++)
const int INF = 0x3f3f3f3f;
const int e_maxn = 150000 * 2;
const int v_maxn = 900;
struct ppp
{
int v,nex,cap,flow,c;
}e[e_maxn];
int head[v_maxn],pre[v_maxn],inq[v_maxn],a[v_maxn],dis[v_maxn];
int tole,N,M,s,t,K;
void make_edge(int u,int v,int cap,int c)
{
e[tole].v = v;e[tole].cap = cap;e[tole].c = c;e[tole].flow = 0;e[tole].nex = head[u]; head[u] = tole++;
}
void add_edge(int u,int v,int cap,int c)
{
make_edge(u,v,cap,c);
make_edge(v,u,0,-c);
}
queue<int> que;
void maxflow_mincost(int s,int t,int &flow,int &cost)
{
int temp,v,u;
while(1)
{
mem(inq,0);
mem(dis,0x3f);
a[s] = INF;
dis[s] = 0;
inq[s] = 1;
que.push(s);
mem(pre,-1);
pre[s] = 0;
while(!que.empty())
{
temp = que.front();
que.pop();
inq[temp] = 0;
for(int i = head[temp];~i;i = e[i].nex)
{
v = e[i].v;
if(e[i].cap > e[i].flow && dis[v] > dis[temp] + e[i].c)
{
dis[v] = dis[temp] + e[i].c;
pre[v] = i;
a[v] = min(a[temp],e[i].cap - e[i].flow);
if(!inq[v]){que.push(v);inq[v] = 1;}
}
}
}
if(pre[t] == -1)break;
flow += a[t];
cost += dis[t] * a[t];
for(u = t;u != s;u = e[pre[u] ^ 1].v)
{
e[pre[u]].flow += a[t];
e[pre[u] ^ 1].flow -= a[t];
}
}
}
int ma[202][202];
int vis_v[202];
void dfss(int u)//找于x相连的点用dfs
{
for(int i = 1; i <= N;i++)
if(!vis_v[i] && ma[u][i])
{
vis_v[i] = 1;
dfss(i);
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
mem(head,-1);
tole = 0;
scanf("%d%d%d",&N,&M,&K);
int cntk = 0;
mem(ma,0);
int a,b,c;
int cost = 9999999;//cost在每次1 x的操作后自减
while(M--)
{
scanf("%d",&a);
if(a == 1){
scanf("%d",&b);
mem(vis_v,0);
vis_v[b] = 1;
dfss(b);
cntk++;
for(int i = 1;i <= N;i++)
if(vis_v[i])
add_edge(i,N + cntk,INF,cost);
cost -= 100;
}else if(a == 2){
scanf("%d%d",&b,&c);
ma[b][c] = 1;
ma[c][b] = 1;
}else if(a == 3){
scanf("%d",&a);
for(int i = 1;i <= a;i++)
{
scanf("%d%d",&b,&c);
ma[b][c] = ma[c][b] = 0;
}
}
}
s = 0,t = N + cntk + 1;
for(int i = 1;i <= N;i++)
add_edge(s,i,1,0);
for(int i = N + 1;i <= N + cntk;i++)
add_edge(i,t,K,0);
int haha = 0,xixi = 0;
maxflow_mincost(s,t,haha,xixi);
int ans[505];
int len = 0;
int summ = 0;
for(int i = N + 1;i <= N + cntk;i++)
{
for(int j = head[i];~j;j = e[j].nex)
{
int v = e[j].v;
if(v == t)
{
summ += e[j].flow;
ans[len++] = e[j].flow;
break;
}
}
}
printf("%d\n",summ);
for(int i = 0;i < len;i++)
{
if(i > 0){
printf(" ");
}
printf("%d",ans[i]);
}
printf("\n");
}
}