hdu 5294 Tricks Device

Tricks Device

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


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
 

Sample Output
  
  
2 6
 
题意: 给出一张无向图,两个问题。
1.找出1-n中间至少删多少条边使1无法到n。
2.找出1-n最短路之外最多还有多少条边(可能有多条最短路)。

分析:
对于第一问,对整张图最短路上建立容量为1的网络,然后跑一边最小割即是答案。
对于第二问,直接找单源最短路。

#include<cstring>
#include<algorithm>
#include<queue>
#include<iostream>
#include<cstdio>
#include<string>

using namespace std;

#define maxn 2005
#define maxm 500005

#define INF 0x3f3f3f3f
int n,m;

int fir[maxn],cnt[maxn],vis[maxn],d[maxn],lv[maxn],iter[maxn];
int u[maxm],v[maxm],w[maxm],nex[maxm],cap[maxm],q[maxm],flow[maxm];
int e_max;

void init()
{
    memset(fir,-1,sizeof fir);
    e_max=0;
}

void add_edge(int s,int t, int c)
{
    int e=e_max++;
    u[e]=s;
    v[e]=t;
    w[e]=c;
    nex[e]=fir[u[e]];
    fir[u[e]]=e;
}

int fir2[maxn];
int u2[maxm],v2[maxm],nex2[maxm];
int e_max2;

void init2()
{
    memset(fir2,-1,sizeof fir2);
    e_max2=0;
}

void add_edge2(int s,int t, int c)
{
    int e=e_max2++;
    u2[e]=s;
    v2[e]=t;
    cap[e]=c;
    nex2[e]=fir2[u2[e]];
    fir2[u2[e]]=e;
}


int bfs_spfa()
{
    memset(cnt,0x3f,sizeof cnt);
    memset(d,0x3f,sizeof d);
    memset(vis,0,sizeof vis);

    queue<int>que;

    vis[1]=1;
    cnt[1]=0;
    d[1]=0;

    que.push(1);

    while(que.size())
    {
        int k=que.front();
        que.pop();
        vis[k]=0;

        for(int i=fir[k]; ~i; i=nex[i])
        {
            int s=u[i];
            int e=v[i];

            if(d[s]+w[i]==d[e]) cnt[e]=min(cnt[s]+1,cnt[e]);
            if(d[s]+w[i]<d[e])
            {
                d[e]=d[s]+w[i];
                cnt[e]=cnt[s]+1;
                if(!vis[e])
                {
                    vis[e]=1;
                    que.push(e);
                }
            }
        }
    }
    return m-cnt[n];
}

void build_G()
{
    init2();
    int e=e_max;
    for(int i=0; i<e; i++)
    {
        int x=u[i];
        int y=v[i];
        if(d[x]+w[i]==d[y])
        {
            add_edge2(x,y,1);
            add_edge2(y,x,0);
        }
    }
}

void dinic_bfs(int s)
{
    int f,r;
    memset(lv,-1,sizeof lv);
    q[f=r=0]=s;
    lv[s]=0;

    while(f<=r)
    {
        int k=q[f++];
        for(int i=fir2[k]; ~i; i=nex2[i])
        {
            int x=u2[i];
            int y=v2[i];

            if(cap[i]>flow[i]&&lv[y]<0)
            {
                lv[y]=lv[x]+1;
                q[++r]=y;
            }
        }
    }
}

int dinic_dfs(int s,int t,int f)
{
    if(s==t) return f;

    for(int &i=iter[s]; ~i; i=nex2[i])
    {
        int x=u2[i];
        int y=v2[i];
        if(cap[i]>flow[i]&&lv[s]<lv[y])
        {
            int d=dinic_dfs(y,t,min(cap[i]-flow[i],f));
            if(d>0)
            {
                flow[i]+=d;
                flow[1^i]-=d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s,int t)
{
    memset(flow,0,sizeof flow);
    int total=0;

    while(1)
    {
        dinic_bfs(s);
        if(lv[t]<0) return total;

        memcpy(iter,fir2,sizeof iter);
        int f;
        while((f=dinic_dfs(s,t,INF))>0)
        {
            total+=f;
        }
    }
    return total;

}

int main()
{
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        init();
        for(int i=0; i<m; i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            add_edge(a,b,c);
            add_edge(b,a,c);
        }

        int ans2=bfs_spfa();
        // printf("%d\n",ans2);
        build_G();
        int ans1=max_flow(1,n);

        printf("%d %d\n",ans1,ans2);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值