BZOJ 1001 狼抓兔子

对偶图

这是一道非常玄学的题目。。
有两种方法。。因为题面特别裸,一看就是最小割,再一看数据,心凉了一半,不过我也去试了下//
dinic是可以在洛谷上不开O2过的,但是在BZOJ上过不了。。然后我去学了下对偶图的概念。
平面图有一个性质: 最平面图大流=最小割=对应的对偶图的最短路径。
然后就开始了令人绝望的建图过程。。。。
大概就是把平面图的任意对角做延长线,将平面分成两部分,一部分当成起点,一部分当成终点。
对于原图的每一条边,都会分割两个区域,我们把这两个区域看成点,原图的边权看成两个区域之间的边权。
这样就建成了对偶图。。说起来很简单,但是这道题的建图。。。。(毒瘤)
我死啃了3个小时对着题解把图建完,去洛谷一交,发现TLE。。。我差点把电脑摔了,还好BZOJ过了。。

先是dinic的代码

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int X = 0, w = 0; char ch = 0;
    while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
    while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}
const int N = 100005;
int n, m, cnt, s, t, head[N<<4], depth[N<<4];
struct { int v, next, f; } edge[N<<10];

inline int hashpos(int x, int y){
    return (x - 1) * m + y;
}

inline void addEdge(int a, int b, int f){
    edge[cnt].v = b, edge[cnt].f = f, edge[cnt].next = head[a], head[a] = cnt ++;
    edge[cnt].v = a, edge[cnt].f = f, edge[cnt].next = head[b], head[b] = cnt ++;
}

inline bool bfs(){
    full(depth, 0);
    queue<int> q;
    q.push(s), depth[s] = 1;
    while(!q.empty()){
        int cur = q.front(); q.pop();
        for(int i = head[cur]; i != -1; i = edge[i].next){
            int u = edge[i].v;
            if(!depth[u] && edge[i].f > 0){
                depth[u] = depth[cur] + 1;
                q.push(u);
            }
        }
    }
    return depth[t] != 0;
}

inline int dfs(int cur, int a){
    if(cur == t) return a;
    int flow = 0;
    for(int i = head[cur]; i != -1; i = edge[i].next){
        int u = edge[i].v;
        if(depth[u] == depth[cur] + 1 && edge[i].f > 0){
            int k = dfs(u, min(edge[i].f, a));
            if(k > 0) a -= k, flow += k, edge[i].f -= k, edge[i^1].f += k;
        }
        if(!a) break;
    }
    if(a) depth[cur] = -1;
    return flow;
}

inline int dinic(){
    int ret = 0;
    while(bfs()){
        ret += dfs(s, INF);
    }
    return ret;
}

int main(){

    full(head, -1);
    n = read(), m = read();
    s = 1, t = hashpos(n, m);
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j < m; j ++){
            int x = read();
            addEdge(hashpos(i, j), hashpos(i, j + 1), x);
        }
    }
    for(int i = 1; i < n; i ++){
        for(int j = 1; j <= m; j ++){
            int x = read();
            addEdge(hashpos(i, j), hashpos(i + 1, j), x);
        }
    }
    for(int i = 1; i < n; i ++){
        for(int j = 1; j < m; j ++){
            int x = read();
            addEdge(hashpos(i, j), hashpos(i + 1, j + 1), x);
        }
    }
    printf("%d\n", dinic());
}

然后是dijkstra

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int X = 0, w = 0; char ch = 0;
    while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
    while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}
const int N = 1000005;
int n, m, cnt, head[N<<2], s, t, dist[N<<2];
bool vis[N<<2];
struct Edge { int v, next, w; } edge[N<<3];
 
void addEdge(int a, int b, int c){
    edge[cnt].v = b, edge[cnt].w = c, edge[cnt].next = head[a], head[a] = cnt ++;
    edge[cnt].v = a, edge[cnt].w = c, edge[cnt].next = head[b], head[b] = cnt ++;
}
 
int dijkstra(){
    full(dist, INF);
    priority_queue< pair<int, int>, vector< pair<int, int> >, greater< pair<int, int> > > pq;
    dist[s] = 0;
    pq.push(make_pair(dist[s], s));
    while(!pq.empty()){
        int cur = pq.top().second, d = pq.top().first; pq.pop();
        if(vis[cur]) continue;
        vis[cur] = true;
        for(int i = head[cur]; i != -1; i = edge[i].next){
            int u = edge[i].v;
            if(dist[u] > d + edge[i].w){
                dist[u] = d + edge[i].w;
                pq.push(make_pair(dist[u], u));
            }
        }
    }
    return dist[t];
}
 
int main(){
 
    full(head, -1);
    n = read(), m = read();
    s = 2 * (n - 1) * (m - 1) + 1, t = s + 1;
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j < m; j ++){
            int x = read();
            if(i == 1) addEdge(s, j, x);
            else if(i == n) addEdge((2 * (n - 1) - 1) * (m - 1) + j, t, x);
            else addEdge((2 * (i - 1) - 1) * (m - 1) + j, 2 * (i - 1) * (m - 1) + j, x);
        }
    }
    for(int i = 1; i < n; i ++){
        for(int j = 1; j <= m; j ++){
            int x = read();
            if(j == 1) addEdge((2 * i - 1) * (m - 1) + 1, t, x);
            else if(j == m) addEdge(s, (2 * i - 1) * (m - 1), x);
            else addEdge(2 * (i - 1) * (m - 1) + j - 1, (2 * (i - 1) + 1) * (m - 1) + j, x);
        }
    }
    for(int i = 1; i < n; i ++){
        for(int j = 1; j < m; j ++){
            int x = read();
            addEdge(2 * (i - 1) * (m - 1) + j, 2 * (i - 1) * (m - 1) + j + (m - 1), x);
        }
    }
    printf("%d\n", dijkstra());
    return 0;
}
posted @ 2019-04-09 21:43  清楚少女ひなこ  阅读(131)  评论(0编辑  收藏  举报