[bzoj1001]狼爪兔子[平面图的最小割等于其对偶图的最短路]

一定要仔细算内存,,,又少写一个零。。

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 template<const int _n,const int _m>
 6 struct Edge
 7 {
 8     struct Edge_base { int    to,next,w; }e[_m]; int    cnt,p[_n];
 9     void    insert(const int x,const int y,const int z)
10     { e[++cnt].to=y; e[cnt].next=p[x]; e[cnt].w=z; p[x]=cnt; }
11     void    clear() { cnt=1,memset(p,0,sizeof(p)); }
12     Edge() { clear(); } int    start(const int x) { return p[x]; }
13     Edge_base&    operator[](const int x) { return e[x]; }
14 };
15 
16 int    n,m,Dis[2100000];
17 bool    visited[2100000];
18 Edge<2100000,11000000>e;
19 
20 void    Dijkstra(const int S)
21 {
22     int    i,t,temp;
23     typedef    pair<int,int>    PII;
24     priority_queue<PII,vector<PII>,greater<PII> >Q;
25     memset(Dis,0x3f,sizeof(Dis));
26     Dis[S]=0;
27     Q.push(make_pair(0,S));
28     while(!Q.empty())
29     {
30         t=Q.top().second;Q.pop();
31         if(visited[t])continue;
32         visited[t]=true;
33         for(i=e.start(t);i;i=e[i].next)
34         {
35             temp=e[i].to;
36             if(Dis[temp]>Dis[t]+e[i].w)
37             {
38                 Dis[temp]=Dis[t]+e[i].w;
39                 Q.push(make_pair(Dis[temp],temp));
40             }
41         }
42     }
43     return ;
44 }
45 
46 int    Get(const int x,const int y,const int z)
47 {
48     if(x<1 || y>=m)return (n-1)*(m-1)*2+2;
49     if(x>=n || y<1)return 1;
50     return ((x-1)*(m-1)+y)<<1|z;
51 }
52 
53 int main()
54 {
55     int    i,j,x;
56 
57     scanf("%d%d",&n,&m);
58 
59     for(i=1;i<=n;++i)
60     {
61         for(j=1;j<m;++j)
62         {
63             scanf("%d",&x);
64             e.insert(Get(i,j,1),Get(i-1,j,0),x);
65             e.insert(Get(i-1,j,0),Get(i,j,1),x);
66         }
67     }
68 
69     for(i=1;i<n;++i)
70     {
71         for(j=1;j<=m;++j)
72         {
73             scanf("%d",&x);
74             e.insert(Get(i,j-1,1),Get(i,j,0),x);
75             e.insert(Get(i,j,0),Get(i,j-1,1),x);
76         }
77     }
78 
79     for(i=1;i<n;++i)
80     {
81         for(j=1;j<m;++j)
82         {
83             scanf("%d",&x);
84             e.insert(Get(i,j,0),Get(i,j,1),x);
85             e.insert(Get(i,j,1),Get(i,j,0),x);
86         }
87     }
88 
89     Dijkstra(1);
90 
91     printf("%d
",Dis[(n-1)*(m-1)*2+2]);
92 
93     return 0;
94 }
原文地址:https://www.cnblogs.com/Gster/p/5090538.html