Path(最短路 + 最大流求最小割)

Years later, Jerry fell in love with a girl, and he often walks for a long time to pay visits to her. But, because he spends too much time with his girlfriend, Tom feels neglected and wants to prevent him from visiting her. 
After doing some research on the neighbourhood, Tom found that the neighbourhood consists of exactly nn houses, and some of them are connected with directed road. To visit his girlfriend, Jerry needs to start from his house indexed 11 and go along the shortest path to hers, indexed nn. 
Now Tom wants to block some of the roads so that Jerry has to walk longer to reach his girl's home, and he found that the cost of blocking a road equals to its length. Now he wants to know the minimum total cost to make Jerry walk longer. 
Note, if Jerry can't reach his girl's house in the very beginning, the answer is obviously zero. And you don't need to guarantee that there still exists a way from Jerry's house to his girl's after blocking some edges.

Input

The input begins with a line containing one integer T(1≤T≤10), the number of test cases. 
Each test case starts with a line containing two numbers n,m(1≤n,m≤10000)n,m(1≤n,m≤10000), the number of houses and the number of one-way roads in the neighbourhood. 
mm lines follow, each of which consists of three integers x,y,c(1≤x,y≤n,1≤c≤109)x,y,c(1≤x,y≤n,1≤c≤109), denoting that there exists a one-way road from the house indexed xx to yy of length cc.

Output

Print T lines, each line containing a integer, the answer.

Sample Input

1
3 4
1 2 1
2 3 1
1 3 2
1 3 3

Sample Output

3

2019杭电多校第一场第五题:http://acm.hdu.edu.cn/contests/contest_show.php?cid=848

题意:T组数据,每组数据有n个顶点,m条有向边,接下来m行,每行u,v,w。u到v点的距离为w。

           问:如果删除一些边,使得从1号点到n号点的最短距离变大,问最少需要删除多长的边。如果本身就不联通输出0.

 

思路:首先我们的目标是要将最短路径边长,那么第一步就是要求最短路了,我们目的是破坏最短路径。考虑到最短路可能存在多条路径,所以我们将所有能够形成最短路径的边重新构成一张图,在这个新图上我们让1和n不连通即可,也就是求图最小割,根据最小割最大流定理(最小割=最大流),也就是求这张图的最大流。


过程如下:为了处理多种路径的情况:可以从起点s(1)正向跑一遍Bellman_Ford,记录起点到达每一个点的最短距离Dis[i],然后在从终点t(n)反向跑一遍Bellman_Ford,记录从终点到达每一个点的最短距离Dir[i]。对于每一条边edge(u,v) 如果Dis[u] + d(u,v) +Dir[v] == Dis[n],也就是说这条边是最短路径中的一条可行边,将此边加入新图中即可。接下来就是求最大流了,我是在网上找的ISAP板子。

代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll int INF = 0x3f3f3f3f3f3f3f3fLL;
const int MAXN = 20010;
const int MAXM = 20010;
struct Edge
{
    int to,next;
    ll cap,flow;
}edge[MAXM];//注意是MAXM

int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];

ll Dis[MAXN],Dir[MAXN];
int n,m,book[MAXN];
int U[MAXN],V[MAXN],W[MAXN],first1[MAXN],Next1[MAXN],first2[MAXN],Next2[MAXN];

void init()
{
    tol = 0;
    memset(head,-1,sizeof(head));
    memset(first1,-1,sizeof(first1));
    memset(first2,-1,sizeof(first2));
}
void add(int x,int y,int tot)
{
    Next1[tot]=first1[x];//正向边
    first1[x]=tot;
    Next2[tot]=first2[y];//反向边
    first2[y]=tot;
}
bool Bellman_Ford(int s,int *first,int *Next,int *u,int *to,ll *dis)
{
    memset(book,0,sizeof(book));
    queue<int>Q;
    book[s]=1,dis[s]=0;
    Q.push(s);
    while(!Q.empty())
    {
        int p=Q.front();
        Q.pop();
        for(int i=first[p];i!=-1;i=Next[i])
        {
            if(dis[to[i]]>dis[u[i]]+W[i])
            {
                dis[to[i]]=dis[u[i]]+W[i];
                if(book[to[i]]==0)
                {
                    book[to[i]]=1;
                    Q.push(to[i]);
                }
            }
        }
        book[p]=0;
    }
    return dis[n]==INF;
}

void addedge(int u,int v,int w,int rw=0)
{
    edge[tol].to = v;edge[tol].cap = (ll)w;edge[tol].next = head[u];
    edge[tol].flow = 0;head[u] = tol++;
    edge[tol].to = u;edge[tol].cap = (ll)rw;edge[tol].next = head[v];
    edge[tol].flow = 0;head[v]=tol++;
}

//输入参数:起点,终点,点的总数
//点的编号没有影响,只要点的总数

ll sap(int start,int end ,int N) //求最小割,即最大流
{
    memset(gap,0,sizeof(gap));
    memset(dep,0,sizeof(dep));
    memcpy(cur,head,sizeof(head));
    int u=start;
    pre[u]=-1;
    gap[0]=N;
    ll ans=0;
    while(dep[start]<N)
    {
        if(u==end)
        {
            ll Min=INF;
            for(int i = pre[u];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[u];i != -1; i = pre[edge[i^1].to])
            {
                edge[i].flow += Min;
                edge[i^1].flow -= Min;
            }
            u=start;
            ans+=Min;
            continue;
        }
        bool flag=false;
        int v;
        for(int i = cur[u]; i != -1;i = edge[i].next)
        {
            v = edge[i].to;
            if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u])
            {
                flag = true;
                cur[u] = pre[v] = i;
                break;
            }
        }
        if(flag)
        {
            u=v;
            continue;
        }
        int Min=N;
        for(int i = head[u]; i != -1;i = edge[i].next)
            if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
            {
                Min = dep[edge[i].to];
                cur[u] = i;
            }
        gap[dep[u]]--;
        if(!gap[dep[u]]) return ans;
        dep[u]=Min+1;
        gap[dep[u]]++;
        if(u!=start)u=edge[pre[u]^1].to;
    }
    return ans;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            Dis[i]=Dir[i]=INF;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&U[i],&V[i],&W[i]);
            add(U[i],V[i],i);
        }
        if(Bellman_Ford(1,first1,Next1,U,V,Dis))printf("0\n");
        else
        {
            int ans=0;
            bool a=Bellman_Ford(n,first2,Next2,V,U,Dir); //利用反向边,跑出从n点到达任意点的最小距离
            memset(book,0,sizeof(book));
            for(int i=1;i<=m;i++)
            {
                if(Dis[U[i]]+W[i]+Dir[V[i]]==Dis[n]) //如果 Dis[u] + d(u,v) + Dir[v]  == Dis[n],也就是说edge(u,v)是最短路上的边的一种情况
                {
                    addedge(U[i],V[i],W[i]);   //重新建图
                    if(!book[U[i]])ans++,book[U[i]]=1;//算这个图中有多少个点
                    if(!book[V[i]])ans++,book[V[i]]=1;
                }
            }
            ll cnt=sap(1,n,ans);//求最大流
            printf("%lld\n",cnt);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值