sgu 252 Railway Communication

题意:

就是给你一个带权的DAG,求把所有点都覆盖的路径最少有多少条,且同时保证权值最小,并输出路径方案!

思路:

DAG上的最小点覆盖,直接上二分图匹配就行了,但是要求权值最小,一种方法就是转化为费用流来求解,另外就是KM算法(可以来求解最大或最小权值)。

另外WA 6 的话,就是注意一下输出的先后顺序,也就会是找二分图中没有入边的点开始dfs输出(开始傻逼的加了个vis数组判重复访问就结果跪了1h QAQ)

代码:
#include <cstdio>
#include <cstring>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int>  P;
const int N = 210,M = 2800 ,MOD=7+1e9;

struct edge
{
    int to,next,cap,flow,cost;
}e[M];

int uN,head[N],tot;
int pre[N],dis[N];
bool vis[N];
void init()
{
    memset(head,-1,sizeof(head));
    tot=0;
}
void addedge(int u,int v,int cap,int cost)
{
    e[tot].to=v , e[tot].cap=cap , e[tot].cost=cost , e[tot].flow=0;
    e[tot].next=head[u] , head[u]=tot++;
    e[tot].to=u , e[tot].cap=0 , e[tot].cost=-cost , e[tot].flow=0;
    e[tot].next=head[v] , head[v]=tot++;
}
bool spfa(int s,int t)
{
    queue<int> q;
    for(int i=0 ; i<uN ;i++)
    {
        dis[i]=INF , vis[i]=0 , pre[i]=-1;
    }
    dis[s] = 0;
    vis[s] = 1;
    q.push(s);

    while(!q.empty())
    {
        int u=q.front(); q.pop();
        vis[u] = 0;
        for(int i = head[u]; i!=-1 ;i = e[i].next)
        {
            int v = e[i].to;
            if(e[i].cap > e[i].flow && dis[v] > dis[u] + e[i].cost){
                dis[v] = dis[u] + e[i].cost;
                pre[v] = i;
                if(!vis[v]) {
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
    return pre[t] != -1;
}
int MincostMaxflow(int s,int t,int &cost)
{
    int flow = 0;
    cost = 0;
    while(spfa(s,t))
    {
        int Min_f = INF;
        for(int i = pre[t];i != -1 ;i = pre[e[i^1].to])
        {
            if(Min_f> e[i].cap-e[i].flow)
                Min_f = e[i].cap-e[i].flow;
        }
        for(int i = pre[t];i != -1 ;i = pre[e[i^1].to])
        {
            e[i].flow += Min_f;
            e[i^1].flow -= Min_f;
            cost += Min_f*e[i].cost;
        }
        flow += Min_f;
    }
    return flow;
}

int path[N], cnt, n;
int indu[N];
bool tp;
void dfs1(int v)
{
    if(tp) path[cnt++] = v;
    for(int i = head[v];i != -1 ;i = e[i].next) {
        if(e[i].flow == 1) {
            int u = e[i].to - n;
            if(u >= 1 && u <= n) {
                indu[u] ++;
                dfs1(u);
            }
        }
    }
}
int main()
{
    int m;
    init();
    scanf("%d%d",&n,&m);
    int S = 0, T = 2*n + 1;
    uN = T + 1;
    for(int i = 0;i < m;i ++) {
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        addedge(u,v+n,1,w);
    }
    for(int i = 1;i <= n;i ++) {
        addedge(S,i,1,0);
        addedge(i+n,T,1,0);
    }
    int cost = 0;
    int res = n - MincostMaxflow(S,T,cost);
    printf("%d %d\n", res, cost);
    tp = 0;
    for(int i = 1;i <= n;i ++) {
        dfs1(i);
    }
    tp = 1;
    for(int i = 1;i <= n;i ++) {
        if(indu[i] == 0) {
            cnt = 0;
            dfs1(i);
            printf("%d",cnt);
            for(int j = 0;j < cnt;j ++) {
                printf(" %d",path[j]);
            }
            printf("\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值