icontofig | 发布于 2019-02-24 16:33:29 | 阅读量 232 | 网络流 费用流 最大流

## 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).

```1
5 6 2
2 1 2
2 1 3
1 1
1 2
3 1 1 2
1 2```

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

## 代码

```#include <bits/stdc++.h>
using namespace std;
const int maxn = 255;
const int maxk = 255;
const int maxm = 505;
int get_num(){
int num = 0;
char c;
bool flag = false;
while((c = getchar()) == ' ' || c == '\r' || c == '\n');
if(c == '-')
flag = true;
else num = c - '0';
while(isdigit(c = getchar()))
num = num * 10 + c - '0';
return (flag ? -1 : 1) * num;
}
struct edge{
int fr,to,cap,dis,id;
edge(int u,int v,int c,int d) : fr(u),to(v),cap(c),dis(d){};
};
const int INF = 1e9+7;
vectore[maxn+maxm];
vectorg;
vectorconnect;
int mp[maxn][maxn];
int d[maxn+maxm],vis[maxn+maxm],pr[maxn+maxm];
int T,s,t;
int n,m,k;
int cnt,now;
int tot = 0;
void init(){
cnt = 0;
for(int i = 0;i <= n+m+1;++i){
e[i].clear();
}
now = INF;
cnt = 0;tot = 0;
memset(pr,-1,sizeof(pr));
g.clear();
memset(mp,0,sizeof(mp));
return;
}
bool SPFA(int s,int t){
for(int i = s;i <= t;++i){
d[i] = INF;
pr[i] = -1;
vis[i] = 0;
}
queueq;
d[s] = 0;
vis[s] = 1;
q.push(s);
while(!q.empty()){
int x = q.front();
q.pop();
vis[x] = 0;
for(int i = 0;i < e[x].size();++i){
int v = e[x][i];
if(d[g[v].to] > d[x] + g[v].dis && g[v].cap > 0){
pr[g[v].to] = v;
d[g[v].to] = d[x] + g[v].dis;
if(!vis[g[v].to]){
vis[g[v].to] = 1;
q.push(g[v].to);
}
}
}
}
return (pr[t] != -1);
}
int mcmf(int s,int t){
int flow = 0,cost = 0;
while(SPFA(s,t)){
int f = INF;
for(int i = pr[t];i != -1;i = pr[g[i].fr]){
f = min(f,g[i].cap);
}
//        for(int i = 0;i <= n+cnt+1;++i)
//            printf("%d ",d[i]);
//        printf("\n");
flow += f;
if(d[t] == INF)break;
cost += d[t] * f;
for(int i = pr[t];i != -1;i = pr[g[i].fr]){
g[i].cap -= f;
g[i^1].cap += f;
}
}
return flow;
}
int opt = 0,u,v,p;
void add(int u,int v,int c,int d){
g.push_back(edge(u,v,c,d));
e[u].push_back(tot++);
g.push_back(edge(v,u,0,-d));
e[v].push_back(tot++);
return;
}
void dfs(int x){
for(int i = 1;i <= n;++i){
if(mp[x][i] == 1 && !vis[i]){
connect.push_back(i);
vis[i] = 1;
dfs(i);
}
}
return;
}
int main(){
T = get_num();
while(T--){
n = get_num();
m = get_num();
k = get_num();
init();
memset(mp,0,sizeof(mp));
s = 0;
for(int i = 1;i <= n;++i)
mp[i][i] = 1;
for(int i = 1;i <= m;++i){
opt = get_num();
if(opt == 2){
u = get_num();
v = get_num();
mp[u][v] = 1;
mp[v][u] = 1;
}
else if(opt == 3){
p = get_num();
while(p--){
u = get_num();
v = get_num();
mp[u][v] = mp[v][u] = 0;
}
}
else{
u = get_num();
cnt++;
now -= 100;
connect.clear();
memset(vis,0,sizeof(vis));
dfs(u);
for(int j = 0;j < connect.size();++j){
}
}
}
t = n + cnt + 1;
for(int i = 1;i <= n;++i){
}
int flow = mcmf(s,t);
printf("%d\n",flow);
for(int i = 0;i < e[s].size();++i){
if(i != e[s].size()-1)
printf("%d ",k - g[e[s][i]].cap);
else printf("%d\n",k - g[e[s][i]].cap);
}
}
return 0;
}﻿​```

232 人读过

0 条评论 