HDU 5889(最短路+网络流)

本文介绍了一个关于最短路径的问题,通过构建图模型并结合最短路径算法与最大流算法来解决如何在特定路径上设置障碍以最大化防御效果的同时最小化资源消耗。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Barricade

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1208    Accepted Submission(s): 368


Problem Description
The empire is under attack again. The general of empire is planning to defend his castle. The land can be seen as N towns and M roads, and each road has the same length and connects two towns. The town numbered 1 is where general's castle is located, and the town numbered N is where the enemies are staying. The general supposes that the enemies would choose a shortest path. He knows his army is not ready to fight and he needs more time. Consequently he decides to put some barricades on some roads to slow down his enemies. Now, he asks you to find a way to set these barricades to make sure the enemies would meet at least one of them. Moreover, the barricade on the i -th road requires wi units of wood. Because of lacking resources, you need to use as less wood as possible.
 

Input
The first line of input contains an integer t , then t test cases follow.
For each test case, in the first line there are two integers N(N1000) and M(M10000) .
The i -the line of the next M lines describes the i -th edge with three integers u,v and w where 0w1000 denoting an edge between u and v of barricade cost w .
 

Output
For each test cases, output the minimum wood cost.
 

Sample Input
  
  
1 4 4 1 2 1 2 4 2 3 1 3 4 3 4
 

Sample Output
  
  
4


题意:

有n个点m条边,敌人会从n点到1点的最短路过来。现在要在敌人必定经过的路上放障碍,每条路上的障碍都有花费,求最小花费。

思路:

首先求一遍最短路将除最短路的边删除,然后因为每条最短路上最多必然只会放一个障碍且是花费最小的障碍,因此可以把图看成是一个最大流问题,求每条路上的最大流。


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<vector>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<math.h>
#include<map>
using namespace std;
#define N 10010
int n,m;
int step[N],vis[N];
struct point
{
    int to,w;
    point(int a,int b):to(a),w(b){}
    point(){}
};
struct line //把边定义为结构体,每条边中间储存出发点、终点、容量、流量  
{  
    int from;  
    int to;  
    int cap;  
    int flow;  
    line(int f,int t,int c,int ff):from(f),to(t),cap(c),flow(ff){}  
    line(){}
};  
vector<point>e[N];
void spfa(int x)
    {
        for(int i=1;i<=n;i++)
            step[i]=N;
        step[x]=0;
        vis[x]=1;
        queue<int>q;
        q.push(x);
        while(!q.empty())
        {
            int t=q.front();
            q.pop();
            vis[t]=0;
            for(int i=0;i<e[t].size();i++)
            {
                if(step[e[t][i].to]>step[t]+1)
                    {
                        step[e[t][i].to]=step[t]+1;
                        if(!vis[e[t][i].to])
                        {
                            vis[e[t][i].to]=1;
                            q.push(e[t][i].to);
                        }
                }
            }
        }
    }
struct Dinic  
{  
    int s,t;  
    vector<line>edges;        //边数的两倍  
    vector<int> G[N];      //邻接表,G[i][j]表示结点i的第j条边在e数组中的序号  
    bool vis[N];           //BFS使用  
    int d[N];              //从起点到i的距离  
    int cur[N];            //当前弧下标  
    void init()  
    {  
       for (int i=0;i<=n+1;i++)  
           G[i].clear();  
       edges.clear();  
    }  
    void AddEdge(int from,int to,int cap)  
    {  
        edges.push_back(line(from,to,cap,0));  
        edges.push_back(line(to,from,0,0));        //反向弧  
        int mm=edges.size();  
        G[from].push_back(mm-2);  
        G[to].push_back(mm-1);  
    }  
    bool BFS()  
    {  
        memset(vis,0,sizeof(vis));  
        queue<int>q;  
        q.push(s);  
        d[s]=0;  
        vis[s]=1;  
        while (!q.empty())  
        {  
            int x = q.front();q.pop();  
            for (int i = 0;i<G[x].size();i++)  
            {  
                line &e = edges[G[x][i]];  
                if (!vis[e.to] && e.cap > e.flow)  
                {  
                    vis[e.to]=1;  
                    d[e.to] = d[x]+1;  
                    q.push(e.to);  
                }  
            }  
        }  
        return vis[t];  
    }  
  
    int DFS(int x,int a)  
    {  
        if (x==t || a==0)  
            return a;  
        int flow = 0,f;  
        for(int &i=cur[x];i<G[x].size();i++)  
        {  
            line &e = edges[G[x][i]];  
            if (d[x]+1 == d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0)  
            {  
                e.flow+=f;  
                edges[G[x][i]^1].flow-=f;  
                flow+=f;  
                a-=f;  
                if (a==0)  
                    break;  
            }  
        }  
        return flow;  
    }  
  
    int Maxflow(int s,int t)  
    {  
        this->s=s;  
        this->t=t;  
        int flow = 0;  
        while (BFS())  
        {
            memset(cur,0,sizeof(cur));  
            flow+=DFS(s,N);  
        }
        return flow;  
    }  
}dc;  
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            e[i].clear();
        for(int i=1;i<=n;i++)
            step[i]=N;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            e[u].push_back(point(v,w));
            e[v].push_back(point(u,w));
        }
        spfa(1);
        dc.init();
        for(int i=1;i<=n;i++)
            for(int j=0;j<e[i].size();j++)
                if(step[i]+1==step[e[i][j].to])
                    dc.AddEdge(i,e[i][j].to,e[i][j].w);
        printf("%d\n",dc.Maxflow(1,n));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值