HDU 5352 MZL's City(费用流)

MZL's City

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1227    Accepted Submission(s): 454


 

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.

题意:给你n,m,k。表示城市有n个,接下来m个操作,每个操作有三种:

1 x    修复x所在的连通块的不超过k个城市

2 x y 建无向边(x,y)

3  z  z条边(x,y) 拆除z条无向边。

刚开始所有城市都被炸毁,没有任何边。修每次修复操作后最多最终能修复多少个城市。

输出能修复的城市数,和每个修复操作  修复的城市数。(字典序最小)

网上说有三种解法,网络流,二分图匹配,费用流。

我能想到的只有费用流了。。。

因为字典序最小,所以越往后的修复操作费用越小一些。然后对每个修复操作 暴搜这次修复操作找出能修复的城市。

代码:

#pragma comment(linker,"/STACK:102400000,102400000")
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1010;
const int MAXM = 100010;
const int INF = 0x3f3f3f3f;
typedef long long ll;
struct Edge
{
	int to, next, cap, flow, cost;
} edge[MAXM];
int head[MAXN], tol;
int pre[MAXN], dis[MAXN];
bool vis[MAXN];
int N,n,m,k;//节点总个数,节点编号从0~N-1
void add(int u, int v, int cap, int cost)
{
	edge[tol].to = v;
	edge[tol].cap = cap;
	edge[tol].cost = cost;
	edge[tol].flow = 0;
	edge[tol].next = head[u];
	head[u] = tol++;
	edge[tol].to = u;
	edge[tol].cap = 0;
	edge[tol].cost = -cost;
	edge[tol].flow = 0;
	edge[tol].next = head[v];
	head[v] = tol++;
}
bool spfa(int s, int t)
{
	queue<int>q;
	for (int i = 0; i < N; i++)
	{
		dis[i] = INF;
		vis[i] = false;
		pre[i] = -1;
	}
	dis[s] = 0;
	vis[s] = true;
	q.push(s);
	while (!q.empty())
	{
		int u = q.front();
		q.pop();
		vis[u] = false;
		for (int i = head[u]; i != -1; i = edge[i].next)
		{
			int v = edge[i].to;
			if (edge[i].cap > edge[i].flow &&
			        dis[v] > dis[u] + edge[i].cost )
			{
				dis[v] = dis[u] + edge[i].cost;
				pre[v] = i;
				if (!vis[v])
				{
					vis[v] = true;
					q.push(v);
				}
			}
		}
	}
	if (pre[t] == -1) return false;
	else return true;
}
//返回的是最大流,cost存的是最小费用
int minCostMaxflow(int s, int t, int &cost)
{
	int flow = 0;
	cost = 0;
	while (spfa(s, t))
	{
		int Min = INF;
		for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to])
		{
			if (Min > edge[i].cap - edge[i].flow)
				Min = edge[i].cap - edge[i].flow;
		}
		for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to])
		{
			edge[i].flow += Min;
			edge[i ^ 1].flow -= Min;
			cost += edge[i].cost * Min;
		}
		flow += Min;
	}
	return flow;
}
int sum,sumin,cnt;
int g[MAXN][MAXN],in[MAXN][MAXN];
vector<int>vc[MAXM];
void dfs(int cnt,int u)
{
    vc[cnt].push_back(u);
    in[cnt][u]=1;
    for(int i=1;i<=n;i++)
    {
        if(!g[u][i]||in[cnt][i]) continue;
        dfs(cnt,i);
    }
}
void build(int s,int t)
{
    for(int i=1;i<=cnt;i++)
    add(s,i,k,cnt+1-i);
    for(int i=1;i<=n;i++)
    add(i+cnt,t,1,0);
    for(int i=1;i<=cnt;i++)
    {
        for(int j=0;j<vc[i].size();j++)
        {
            int v=vc[i][j];
            add(i,v+cnt,1,0);
        }
    }
}
void init()
{
	tol=0;
	memset(head,-1,sizeof(head));
    memset(g,0,sizeof(g));
    memset(in,0,sizeof(in));
    for(int i=0;i<=m*2+1;i++)vc[i].clear();
}
int main(){
    int Tc,cas=1,S,T;
      scanf("%d",&Tc);
      {
          while(Tc--)
          {
          sum=0;sumin=0;
          scanf("%d%d%d",&n,&m,&k);
          S=0;cnt=0;
          init();
          for(int i=0;i<m;i++)
          {
              int id;
              scanf("%d",&id);
              if(id==1)
              {
                  int x;
                  scanf("%d",&x);
                  cnt++;
                  dfs(cnt,x);
              }
              else if(id==2)
              {
                  int x,y;
                  scanf("%d%d",&x,&y);
                  g[x][y]=g[y][x]=1;
              }
              else
              {
                  int x,y,z;
                  scanf("%d",&z);
                  while(z--)
                  {
                      scanf("%d%d",&x,&y);
                      g[x][y]=g[y][x]=0;
                  }
              }
          }
          T=n+cnt+1;
          N=T+1;
          build(S,T);
          int ct=0;
          int ans=minCostMaxflow(S,T,ct);
          printf("%d\n",ans);
          for(int i=0;i<cnt*2;i+=2)
          {
              printf("%d%c",edge[i].flow,i==cnt*2-2?'\n':' ');
          }
          }
      }
return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值