hdu 5294 Tricks Device (最小割+最短路径+Dinic模板)

Tricks Device

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


Problem Description
Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the entrance of the tomb while Dumb Zhang’s at the end of it. The tomb is made up of many chambers, the total number is N. And there are M channels connecting the chambers. Innocent Wu wants to catch up Dumb Zhang to find out the answers of some questions, however, it’s Dumb Zhang’s intention to keep Innocent Wu in the dark, to do which he has to stop Innocent Wu from getting him. Only via the original shortest ways from the entrance to the end of the tomb costs the minimum time, and that’s the only chance Innocent Wu can catch Dumb Zhang.
Unfortunately, Dumb Zhang masters the art of becoming invisible(奇门遁甲) and tricks devices of this tomb, he can cut off the connections between chambers by using them. Dumb Zhang wanders how many channels at least he has to cut to stop Innocent Wu. And Innocent Wu wants to know after how many channels at most Dumb Zhang cut off Innocent Wu still has the chance to catch Dumb Zhang.
 

Input
There are multiple test cases. Please process till EOF.
For each case,the first line must includes two integers, N(<=2000), M(<=60000). N is the total number of the chambers, M is the total number of the channels.
In the following M lines, every line must includes three numbers, and use ai、bi、li as channel i connecting chamber ai and bi(1<=ai,bi<=n), it costs li(0<li<=100) minute to pass channel i.
The entrance of the tomb is at the chamber one, the end of tomb is at the chamber N.
 

Output
Output two numbers to stand for the answers of Dumb Zhang and Innocent Wu’s questions.
 

Sample Input
  
  
8 9 1 2 2 2 3 2 2 4 1 3 5 3 4 5 4 5 8 1 1 6 2 6 7 5 7 8 1
 

题意:n个点,m条边,构建有权无向图。求出删去最少条边数可以阻断所有最短路,以
及删出最多条边使得图仍有和原始图的最短路一样长度的最短路存在。
思路:先求出最短路Min。然后通过dist1[i]+dis2[j]+map[j][i]==Min(dist1[i]表示起点到i点
的最短距离,dist2[i]表示终点到i点的最短距离)如果符合的话map[j][i]就是最短路中的一
条边。然后把这些最短路的边建图(流量为1),求出最大流,流量是有多少边权相同的重边,
求出来的最大流就是最小割,也就是阻断所有最短路的最小花费。然后如最大流同样的建
边(边权为1),求出来的最短路dist[n],就是跨越边数最少的最短路的边数了。 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int inf=999999999;
const int maxx=60000+15;
const int maxn=2000+15;

struct node
{
    int v,dis,next;
    node(){}
    node(int vv,int ddis):v(vv),dis(ddis){}
};
struct code
{
    int v,dis,next;
    void fun(int vv,int ddist,int nnext)
    {
        v=vv;
        dis=ddist;
        next=nnext;
    }
}a[maxx*2];

vector <node> G[maxn];
int n,m,cnt,from,to,max_flow,ans,d[2][maxn],dist[maxn];
int head[maxx],s[maxx],t[maxx],val[maxx];
bool vis[maxn];

void initial()
{
    ans=-1;
    from=1,to=n;
    max_flow=cnt=0;
    memset(head,-1,sizeof(head));
    for(int i=0;i<=n;i++)  G[i].clear();
}

void input()
{
    for(int i=0;i<m;i++)
    {
        scanf("%d %d %d",&s[i],&t[i],&val[i]);
        G[s[i]].push_back(node(t[i],val[i]));
        G[t[i]].push_back(node(s[i],val[i]));
    }
}

void spfa(int pos,int u)
{
    memset(vis,0,sizeof(vis));
    for(int i=0;i<=n;i++)  d[pos][i]=inf;
    queue <int>q;
    q.push(u);
    vis[u]=1,d[pos][u]=0;
    while(!q.empty())
    {
        int tmp=q.front(); q.pop();
        vis[tmp]=0;
        for(int i=0;i<G[tmp].size();i++)
        {
            int v=G[tmp][i].v;
            if(d[pos][v]>d[pos][tmp]+G[tmp][i].dis)
            {
                d[pos][v]=d[pos][tmp]+G[tmp][i].dis;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
}

void add(int x,int y,int z)
{
    a[cnt].fun(y,z,head[x]);
    head[x]=cnt++;
    a[cnt].fun(x,0,head[y]);
    head[y]=cnt++;
}

bool bfs()
{
    memset(dist,-1,sizeof(dist));
    dist[from]=0;
    queue<int>q;
    q.push(from);
    while(!q.empty())
    {
        int u=q.front(); q.pop();
        for(int i=head[u];i!=-1;i=a[i].next)
        {
            if(a[i].dis && dist[a[i].v]==-1)
            {
                dist[a[i].v]=dist[u]+1;
                if(a[i].v==to)
                {
                    if(ans==-1)  ans=dist[a[i].v];
                    return true;
                }
                q.push(a[i].v);
            }
        }
    }
    return false;
}

int dfs(int s,int limit)
{
    int cost=0;
    if(s==to)  return limit;
    for(int i=head[s];i!=-1;i=a[i].next)
    {
        int v=a[i].v;
        if(dist[v]==dist[s]+1 && a[i].dis)
        {
            int tmp=min(v,min(limit-cost,a[i].dis));
            if(tmp>0)
            {
                a[i].dis-=tmp;
                a[i^1].dis+=tmp;
                cost+=tmp;
                if(limit==cost)  break;
            }
            else  dist[v]=-1;
        }
    }
    return cost;
}

void dinic()
{
    while(bfs())
    {
        int f=dfs(from,inf);
        if(f==0)  break;
        max_flow+=f;
    }
}

void ready()
{
    spfa(0,1);
    spfa(1,n);
    int Min=d[0][n];
    for(int i=0;i<m;i++)
    {
        if(d[0][s[i]]+d[1][t[i]]+val[i]==Min) add(s[i],t[i],1);
        if(d[0][t[i]]+d[1][s[i]]+val[i]==Min) add(t[i],s[i],1);
    }
    dinic();
    printf("%d %d\n",max_flow,m-ans);
}

int main()
{
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        initial();
        input();
        ready();
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值