HDU 5294 Tricks Device


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


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  


思路很明朗,首先用Spfa跑出最短路。

一、最少需要断几条路到达不了终点?

我们只需要将每条边的流量设为1,然后再图上跑Dinic最大流即可,流量就是答案一。

二、最多断几条路能到达终点 == 最少留几条边到达终点

我们需要再跑最短路的时候记录每条最短路的深度,边数深度最低的最短路就是我们要留下的路。

刚开始一直WA,因为用的是一个类似的题的模板,那个题也是求最短路上的最大流,但是边不限流量,每个节点的流量由题给出,需要拆点建图。在那个模板上修改一直过不了,最后不得已重写了。先建邻接表1,然后跑Spfa,再建邻接表2(最短路),然后在邻接表2上跑Dinic,用的是以前学姐给的Dinic模板,成功A掉此题。代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define inf 0x7fffffff
#define ll long long
#define MAXN 2012
#define MAXM 200000
using namespace std;
struct Eg  //初始边
{
        int u, v, w;
        int next;
} eg[MAXM];
int ecnt;
int pre[MAXN];
void add_eg(int u, int v, int w)
{
        eg[ecnt].u = u, eg[ecnt].v = v;
        eg[ecnt].w = w, eg[ecnt].next = pre[u];
        pre[u] = ecnt++;

        eg[ecnt].u = v, eg[ecnt].v = u;
        eg[ecnt].w = w, eg[ecnt].next = pre[v];
        pre[v] = ecnt++;
}


struct Edge  //最短边
{
        int v, w, f;
        int next;
} edge[MAXM];
int cnt;
int first[MAXN];
void add_edge(int u, int v, int f)
{
        edge[cnt].v = v, edge[cnt].f = f;
        edge[cnt].next = first[u], first[u] = cnt++;
        edge[cnt].v = u, edge[cnt].f = 0;  //增加一条反向弧,容量为0
        edge[cnt].next = first[v], first[v] = cnt++;
}


int dist[MAXN], vis[MAXN];
int dep[MAXN]; //深度
int spfa(int s)
{
        for (int i = 0; i < MAXN; i++)
        {
                dist[i]=inf;
                vis[i]=0;
                dep[i]=inf;
        }
        queue<int> q;
        dist[s]=0;
        dep[s]=0;
        vis[s]=1;
        q.push(s);
        while(!q.empty())
        {
                int u=q.front();
                q.pop();
                vis[u]=0;
                for(int i=pre[u];~i;i=eg[i].next)
                {
                        int v=eg[i].v,w=eg[i].w;
                        if(dist[v]==dist[u]+w)
                        {
                                dep[v]=min(dep[v],dep[u]+1);
                                if(!vis[v])
                                {
                                        vis[v]=1;
                                        q.push(v);
                                }
                        }
                        if(dist[u]+w<dist[v])
                        {
                                dist[v]=dist[u]+w;
                                dep[v]=dep[u]+1;
                                if(!vis[v])
                                {
                                        vis[v]=1;
                                        q.push(v);
                                }
                        }
                }
        }
}

void init()
{
        cnt = 0;
        ecnt = 0;
        memset(first, -1, sizeof(first));
        memset(pre, -1, sizeof(pre));
}

int n, m;
int level[MAXN];
int bfs(int s, int t) //构建层次网络
{
        queue<int> q;
        memset(level, 0, sizeof(level));
        level[s] = 1;
        q.push(s);
        while (!q.empty())
        {
                int x = q.front();
                q.pop();
                if (x == t)
                {
                        return 1;
                }
                for (int e = first[x]; e != -1; e = edge[e].next)
                {
                        int v = edge[e].v, f = edge[e].f;
                        if (!level[v] && f > 0)
                        {
                                level[v] = level[x] + 1;
                                q.push(v);
                        }
                }
        }
        return 0;
}
int dfs(int u, int maxf, int t)//maxf u点可流量
{
        if (u == t)
        {
                return maxf;
        }
        int ret = 0;
        for (int e = first[u]; e != -1; e = edge[e].next)
        {
                int v = edge[e].v, f = edge[e].f;
                if (level[u] + 1 == level[v] && f)
                {
                        int Min = min(maxf - ret, f);
                        f = dfs(v, Min, t);
                        edge[e].f -= f;
                        edge[e ^ 1].f += f;
                        ret += f;
                        if (ret == maxf)
                        {
                                return ret;
                        }
                }
        }
        if (ret == 0)
        {
                level[u] = 0;
        }
        return ret;
}
int Dinic(int s, int t) //Dinic
{
        int ans = 0;
        while (bfs(s, t))
        {
                ans += dfs(s, inf, t);
        }
        return ans;
}

int main()
{
        int n, m;
        while (scanf("%d %d", &n, &m) != EOF)
        {
                init();
                for (int i = 0; i < m; i++)
                {
                        int u, v, w;
                        scanf("%d %d %d", &u, &v, &w);
                        add_eg(u, v, w);
                }
                spfa(1);
                for(int i=1;i<=n;i++)
                {
                        for(int j=pre[i];~j;j=eg[j].next)
                        {
                                int v=eg[j].v,w=eg[j].w;
                                if(dist[v]-dist[i]==w)
                                {
                                        add_edge(i,v,i);
                                }
                        }
                }
                printf("%d %d\n",Dinic(1,n),m-dep[n]);
        }
        return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值