码迷,mamicode.com
首页 > 其他好文 > 详细

【luogu P1343 地震逃生】 题解

时间:2018-08-06 19:15:49      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:amp   ini   include   pac   print   using   ref   head   www   

题目链接: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 地震逃生】 题解

标签:amp   ini   include   pac   print   using   ref   head   www   

原文地址:https://www.cnblogs.com/MisakaAzusa/p/9431876.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!