BZOJ 1001 平面图转对偶图

原图的面转成点,原图的边依旧边,只是连接的是两个面.

对偶图的点数=原图的面数 对偶图的边数=原图的边数(如果原边只属于一个面,则它为环边)

 

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2500005, MAXM = 3500005;
int mindist[MAXN];
bool vis[MAXN];
int to[MAXM << 1], nxt[MAXM << 1], Head[MAXN], ed = 1;
int cost[MAXM << 1];
inline void addedge(int u, int v, int c) {
        to[++ed] = v;
        nxt[ed] = Head[u];
        cost[ed] = c;
        Head[u] = ed;
        to[++ed] = u;
        nxt[ed] = Head[v];
        cost[ed] = c;
        Head[v] = ed;
}
inline void read(int &v) {
        v = 0;
        char c = 0;
        int p = 1;
        while (c < '0' || c > '9') {
                if (c == '-') {
                        p = -1;
                }
                c = getchar();
        }
        while (c >= '0' && c <= '9') {
                v = (v << 3) + (v << 1) + c - '0';
                c = getchar();
        }
        v *= p;
}
struct HeapNode {
        int d, u;
        bool operator < (const HeapNode& rhs) const {
                return d > rhs.d;
        }
} zz;
priority_queue<HeapNode> que;
void Hijkstra(int s) {
        mindist[s] = 0;
        memset(vis, 0, sizeof(vis));
        zz.d = 0, zz.u = s;
        que.push(zz);
        while (!que.empty()) {
                HeapNode x = que.top();
                que.pop();
                int u = x.u;
                if (vis[u] || mindist[u] != x.d) {
                        continue;
                }
                vis[u] = true;
                for (int v, i = Head[u]; i; i = nxt[i]) {
                        v = to[i];
                        if (mindist[v] > mindist[u] + cost[i]) {
                                mindist[v] = mindist[u] + cost[i];
                                //p[v]=u;
                                zz.d = mindist[v], zz.u = v;
                                que.push(zz);
                        }
                }
        }
}
int n, m;
int getnum(int x, int y, int add) {
        return  (x - 1) * (m - 1) * 2 + (y - 1) * 2 + add;
}
int main() {
        int u, v, c;
        read(n), read(m);
        int sum = (n - 1) * (m - 1) * 2 + 2;
        if (n == 1 || m == 1) {
                int ans = INT_MAX;
                if (n == 1) {
                        for (int i = 1; i < m; i++) {
                                read(c);
                                ans = min(ans, c);
                        }
                } else {
                        for (int i = 1; i < n; i++) {
                                read(c);
                                ans = min(ans, c);
                        }
                }
                printf("%d\n", ans);
                return 0;
        }
        for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m - 1; j++) {
                        read(c);
                        if (i == 1) {
                                addedge(sum, getnum(i, j, 1), c);
                        } else if (i == n) {
                                addedge(sum - 1, getnum(i - 1, j, 0), c);
                        } else {
                                addedge(getnum(i, j, 1), getnum(i - 1, j, 0), c);
                        }
                }
        }
        for (int i = 1; i <= n - 1; i++) {
                for (int j = 1; j <= m; j++) {
                        read(c);
                        if (j == 1) {
                                addedge(sum - 1, getnum(i, j, 0), c);
                        } else if (j == m) {
                                addedge(sum, getnum(i, j - 1, 1), c);
                        } else {
                                addedge(getnum(i, j, 0), getnum(i, j - 1, 1), c);
                        }
                }
        }
        for (int i = 1; i <= n - 1; i++) {
                for (int j = 1; j <= m - 1; j++) {
                        read(c);
                        addedge(getnum(i, j, 0), getnum(i, j, 1), c);
                }
        }
        for (int i = 0; i <= sum + 1; i++) {
                mindist[i] = 1e9;
        }
        Hijkstra(sum - 1);
        printf("%d\n", mindist[sum]);
}
View Code

 

posted @ 2018-11-27 16:07  Aragaki  阅读(338)  评论(0编辑  收藏  举报