HDU 5352 MZL's City

最小费用最大流,因为要控制字典序,网络流控制不好了...一直WA,所以用了费用流,时间早的费用大,时间晚的费用少。

构图:

建立一个超级源点和超级汇点。超级源点连向1操作,容量为K,费用为COST,然后COST-1,因为下一次遇到1操作,费用为减少1,即COST-1;

每个1操作连向当前能建造的城市,容量为1,费用为0;

每个城市连向汇点,容量为1,费用为0;

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;

//设置节点数量
const int maxn=1000+10;

const int INF=0x7FFFFFFF;
struct Edge
{
    int from,to,cap,flow,cost;
};
int N,M,K,len,s,t;
vector<Edge> edges;
vector<int> G[maxn];
int inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn];
int use[maxn];
int dis[maxn][maxn];

void init()
{
    for(int i=0; i<maxn; i++) G[i].clear();
    edges.clear();
}

void Addedge(int from,int to,int cap,int cost)
{
    edges.push_back((Edge)
    {
        from,to,cap,0,cost
    });
    edges.push_back((Edge)
    {
        to,from,0,0,-cost
    });
    len=edges.size();
    G[from].push_back(len-2);
    G[to].push_back(len-1);
}

bool BellmanFord(int s,int t,int &flow,int &cost)
{

    for(int i=0; i<maxn; i++) d[i]=INF;

    memset(inq,0,sizeof(inq));
    memset(p,-1,sizeof(p));

    d[s]=0;
    inq[s]=1;
    p[s]=0;
    a[s]=INF;

    queue<int>Q;
    Q.push(s);
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        inq[u]=0;
        for(int i=0; i<G[u].size(); i++)
        {
            Edge& e=edges[G[u][i]];
            if(e.cap>e.flow&&d[e.to]>d[u]+e.cost)
            {
                d[e.to]=d[u]+e.cost;
                p[e.to]=G[u][i];
                a[e.to]=min(a[u],e.cap-e.flow);
                if(!inq[e.to])
                {
                    Q.push(e.to);
                    inq[e.to]=1;
                }
            }
        }
    }
    if(d[t]==INF) return false;
    flow+=a[t];
    cost+=d[t]*a[t];
    int u=t;
    while(u!=s)
    {
        edges[p[u]].flow+=a[t];
        edges[p[u]^1].flow-=a[t];
        u=edges[p[u]].from;
    }
    return true;
}

void Mincost (int s,int t)
{
    int flow=0,cost=0;
    while(BellmanFord(s,t,flow,cost));
    printf("%d\n",flow);
    // printf("最小费用:%d\n",cost);
}

void dfs(int x)
{
    use[x]=1;
    for(int i=1; i<=N; i++)
    {
        if(use[i]==0&&dis[i][x])
            dfs(i);
    }
}

int main()
{
    int TT;
    scanf("%d",&TT);
    while(TT--)
    {
        scanf("%d%d%d",&N,&M,&K);
        //n个节点,m条边
        init();//初始化
        s=0;
        t=N+1;//设置源点和汇点
        memset(dis,0,sizeof(dis));
        int tt=N+2;
        for(int i=1; i<=N; i++) Addedge(i,t,1,0);
        int COST=M+10;
        for(int i=0; i<M; i++)
        {
            int x;
            scanf("%d",&x);
            if(x==1)
            {
                int e;
                memset(use,0,sizeof(use));
                scanf("%d",&e);
                Addedge(s,tt,K,COST);
                COST--;
                dfs(e);
                for(int ii=1; ii<=N; ii++)
                    if(use[ii])
                        Addedge(tt,ii,1,0);
                tt++;
            }
            else if(x==2)
            {
                int l,r;
                scanf("%d%d",&l,&r);
                dis[l][r]=1;
                dis[r][l]=1;
            }
            else
            {
                int e;
                scanf("%d",&e);
                while(e--)
                {
                    int l,r;
                    scanf("%d%d",&l,&r);
                    dis[l][r]=0;
                    dis[r][l]=0;
                }
            }
        }
        Mincost(s,t);
        int flag=0;
        for(int i=0; i<edges.size(); i++)
        {
            if(flag==0&&edges[i].from==s)
            {
                printf("%d",edges[i].flow);
                flag=1;
            }
            else if(edges[i].from==s)
                printf(" %d",edges[i].flow);
        }
        printf("\n");
    }
    return 0;
}

HDU 5352 MZL's City

时间: 2024-08-11 01:34:41

HDU 5352 MZL's City的相关文章

hdu 5352 MZL&#39;s City 【二分图】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5352 题意: 给你n,m,k 表示n个建筑 m次操作,修复操作每次最多修复k个建筑. 有三种操作 1.修复x点周围建筑(<=k) 2.x,y建筑间建边 3.x,y间删边 修复建筑时候拆点建图.反着求最大匹配,保证字典序最小. 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <l

HDU 5352——MZL&#39;s City——————【二分图多重匹配、拆点】

MZL's City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 710    Accepted Submission(s): 245 Problem Description MZL is an active girl who has her own country. Her big country has N cities numb

HDU 5352 MZL&#39;s City(2015 多校 第5场,最小费用最大流)

  MZL's City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 291    Accepted Submission(s): 94 Problem Description MZL is an active girl who has her own country. Her big country has N cities nu

Hdu 5352 MZL&#39;s City (多重匹配)

题目链接: Hdu 5352 MZL's City 题目描述: 有n各节点,m个操作.刚开始的时候节点都是相互独立的,一共有三种操作: 1:把所有和x在一个连通块内的未重建过的点全部重建. 2:建立一条双向路(x,y) 3:又发生了地震,p条路被毁. 问最后最多有多少个节点被重建,输出重建节点的最小字典序. 解题思路: 这几天正好在学匹配,但是昨天下午还是没有看出来这个是匹配题目.看了题解扪心自问了自己三次,是不是傻.就是把每个需要重建的节点x拆成k个点,然后对每个拆分后的点和与拆点在同一连通块

hdu 5352 MZL&#39;s City 最小费用最大流

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5352 题意:M年前有一个国家发生了地震.所有城市和城市之间的路都被摧毁了.现在要重建城市. 给出一个N表示共有N个城市,M表示大地震发生在M年前,K表示每次最多只能重建K个城市. 现在从大地震发生的那年开始,每年可以进行一个操作,也就是总共有M个操作. 1 x表示可以重建和x联通(直接或间接)的城市(包括x本身),每次最多只能重建K个城市 2 x y 表示修建了一条城市x到城市y的路. 3操作给出一

HDOJ 5352 MZL&#39;s City 匈牙利匹配

求年份和城市的匹配 MZL's City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 539    Accepted Submission(s): 180 Problem Description MZL is an active girl who has her own country. Her big country has N

MZL&#39;s City (hdu 5352 最小费用流 ||二分图匹配)

MZL's City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 719    Accepted Submission(s): 251 Problem Description MZL is an active girl who has her own country. Her big country has N cities num

HDU 5344 MZL&#39;s xor (多校)[补7月28]

MZL's xor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 249    Accepted Submission(s): 187 Problem Description MZL loves xor very much.Now he gets an array A.The length of A is n.He wants to k

hdu 5348 MZL&amp;#39;s endless loop

给一个无向图(事实上是有向的.可是没有指定边的方向),你须要指定边的方向,使得每一个点入度和出度相差不超过1. 事实上就是找很多条路径.合起来能走完这个图..先统计各个顶点的度.度为奇数必是起点或终点,否则是中间点或者同为起点和终点. 邻接表建图(建双向),先从每一个奇数度顶点出发找路径,再从偶数度顶点出发找路径.经过的边要删去不然超时. #include <iostream> #include <cstring> #include <cstdio> #include