1. 程式人生 > >【luogu P1343 地震逃生】 題解

【luogu P1343 地震逃生】 題解

clu can true ini cow amp 鏈接 ron empty

題目鏈接:https://www.luogu.org/problemnew/show/P1343

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 10000 + 10;
const int inf = 1e9;
int n, m, x, s, t, deep[maxn], maxflow;
struct edge{
    int next, to, len;
}e[maxn<<2];
int head[maxn], cnt = -1, cur[maxn];
queue<int> q;
void add(int u, int v, int w, bool flag)
{
    e[++cnt].next = head[u];
    e[cnt].to = v;
    if(flag) e[cnt].len = w;
    head[u] = cnt;
}
bool bfs(int s, int t)
{
    memset(deep, 0x7f, sizeof(deep));
    while(!q.empty()) q.pop();
    for(int i = 1; i <= n; i++) cur[i] = head[i];
    q.push(s); deep[s] = 0;
    while(!q.empty())
    {
        int now = q.front(); q.pop();
        for(int i = head[now]; i != -1; i = e[i].next)
        {
            if(deep[e[i].to] > inf && e[i].len)
            {
                deep[e[i].to] = deep[now] + 1;
                q.push(e[i].to);
            }
        }
    }
    if(deep[t] < inf) return true;
    else return false;
}
int dfs(int now, int t, int limit)
{
    if(!limit || now == t) return limit;
    int flow = 0, f;
    for(int i = cur[now]; i != -1; i = e[i].next)
    {
        cur[now] = i;
        if(deep[e[i].to] == deep[now] + 1 && (f = dfs(e[i].to, t, min(e[i].len, limit))))
        {
            flow += f;
            limit -= f;
            e[i].len -= f;
            e[i^1].len += f;
            if(!limit) break;
        }
    }
    return flow;
}
void Dinic(int s, int t)
{
    while(bfs(s, t))
    maxflow += dfs(s, t, inf);
}
int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d%d%d",&n,&m,&x);
    s = 1, t = n;
    for(int i = 1; i <= m; i++)
    {
        int u, v, w;
        scanf("%d%d%d",&u,&v,&w);
        add(u, v, w, 1);
        add(v, u, w, 0);
    }
    Dinic(s, t);
    if(maxflow == 0)
    {
        printf("Orz Ni Jinan Saint Cow!\n");
        return 0;
    }
    int ans;
    if(x % maxflow == 0)
    ans = x/maxflow;
    else
    ans = x/maxflow + 1;
    printf("%d %d",maxflow,ans);
    return 0;
}

【luogu P1343 地震逃生】 題解