【最短路】 HDOJ 3870 Catch the Theves
来源:互联网 发布:通达信macd公式源码 编辑:程序博客网 时间:2024/05/22 11:36
详细解法参考论文————两极相通——浅析最大—最小定理在信息学竞赛中的应用
论文已经讲的很详细了。。。建图跑最短路就可以了。。。
#include <iostream>#include <queue> #include <stack> #include <map> #include <set> #include <bitset> #include <cstdio> #include <algorithm> #include <cstring> #include <climits>#include <cstdlib>#include <cmath>#include <time.h>#define maxn 200005#define maxm 20000005#define eps 1e-10#define mod 1000000007#define INF 0x3f3f3f3f#define lowbit(x) (x&(-x))#define mp make_pair#define ls o<<1#define rs o<<1 | 1#define lson o<<1, L, mid #define rson o<<1 | 1, mid+1, R typedef long long LL;typedef unsigned long long ULL;//typedef int LL;using namespace std;struct Edge{int v, c;Edge *next;}pool[maxm], *H[maxn], *edges;struct node{int u, d;node(int u = 0, int d = 0) : u(u), d(d) {}bool operator < (const node& b) const {return d > b.d;}};priority_queue<node> q;int dist[maxn], vis[maxn];int g[405][405];int n;void addedges(int u, int v, int c){edges->v = v;edges->c = c;edges->next = H[u];H[u] = edges++;}void init(void){edges = pool;memset(H, 0, sizeof H);memset(vis, 0, sizeof vis);memset(dist, 0x3f, sizeof dist);}inline int calc(int i, int j){return (i - 1) * (n - 1) + j;}void read(void){scanf("%d", &n);for(int i = 1; i <= n; i++)for(int j = 1; j <= n; j++)scanf("%d", &g[i][j]);}int dijstra(int s, int t){dist[s] = 0;q.push(node(s, dist[s]));while(!q.empty()) {int u = q.top().u, d = q.top().d;q.pop();if(vis[u]) continue;vis[u] = 1;for(Edge *e = H[u]; e; e = e->next) {if(dist[e->v] > dist[u] + e->c) {dist[e->v] = dist[u] + e->c;q.push(node(e->v, dist[e->v]));}}}return dist[t];}void debug(void){for(int i = 0; i <= n * n + 1; i++) {printf("AA %d ::\n", i);for(Edge *e = H[i]; e; e = e->next) {printf("v = %d c = %d\n", e->v, e->c);}}}void work(void){int s = 0, t = n * n + 1;for(int i = 1; i < n; i++) {for(int j = 1; j < n; j++) {if(i != n-1) {addedges(calc(i, j), calc(i+1, j), g[i+1][j]);addedges(calc(i+1, j), calc(i, j), g[i+1][j]);}if(j != n-1) {addedges(calc(i, j), calc(i, j+1), g[i][j+1]);addedges(calc(i, j+1), calc(i, j), g[i][j+1]);}}}for(int i = 1; i < n; i++) {addedges(calc(1, i), t, g[1][i]);addedges(t, calc(1, i), g[1][i]);}for(int i = 1; i < n; i++) {addedges(calc(i, n-1), t, g[i][n]);addedges(t, calc(i, n-1), g[i][n]);}for(int i = 1; i < n; i++) {addedges(calc(i, 1), s, g[i][1]);addedges(s, calc(i, 1), g[i][1]);}for(int i = 1; i < n; i++) {addedges(calc(n-1, i), s, g[n][i]);addedges(s, calc(n-1, i), g[n][i]);}printf("%d\n", dijstra(s, t));}int main(void){int _;while(scanf("%d", &_)!=EOF) {while(_--) {init();read();work();}}return 0;}
0 0
- 【最短路】 HDOJ 3870 Catch the Theves
- HDU 3870 Catch the Theves 最短路求最小割
- 【HDU】3870 Catch the Theves 对偶图全局最小割 最短路
- HDU - 3870 Catch the Theves(最小割+对偶图==最短路)
- hdu 3870 Catch the Theves(对偶图,最短路,最小割)
- HDU 3870 Catch the Theves
- HDU 3870Catch the Theves
- HDU-3870-Catch the Theves
- HDU 3870 Catch the Theves(对偶图的最小割转最短路)
- HDU3870 Catch the Theves 题解(平面图最小割转最短路)
- hdu 3870 Catch the Theves(平面最小割)
- 平面图s-t最小割 HDU 3870 Catch the Theves
- hdu 3870 Catch the Theves 平面图转对偶图
- HDU 3870 Catch the Theves 2011 Multi-University Training Contest 4 - Host by SDU
- 【最短路】 HDOJ 3873 Invade the Mars
- HDOJ--1596--find the safest road【最短路】
- HDOJ 2680 Choose the best route(最短路,dijkstra)
- HDOJ 题目2680 Choose the best route(最短路)
- SQLite 3 部分语句
- java抽象类
- 指定.net程序在特定.net平台编译运行
- 后台定位提供经纬度,向多个Activity共享数据
- Acdream 1424 Diversion 树链剖分+线段树
- 【最短路】 HDOJ 3870 Catch the Theves
- Ant配置 ANT_HOME is set incorrectly or ant could not be located
- poj 1422&&HDU 1151 Air Raid(最小路径覆盖)
- CCNP路由实验之十七 广域网技术之一
- LeetCode 132 Valid Parentheses
- Myeclipse启动tomcat出现Can't load AMD 64-bit .dll on a IA 32-bit platform
- hadoop各个名词的理解
- hybris学习笔记:Trail ~ Testing the Services
- Java读写xml文件的一些经验(使用dom4j)