Problem Description
A group of thieves is approaching a museum in the country of zjsxzy,now they are in city A,and the museum is in city B,where keeps many broken legs of zjsxzy.Luckily,GW learned the conspiracy when he is watching stars and told it to zjsxzy.
Zjsxzy decided to caught these thieves,and he let the police to do this,the police try to catch them on their way from A to B. Although the thieves might travel this way by more than one group, zjsxzy's excellent police has already gather the statistics that the cost needed on each road to guard it. 
Now ,zjsxzy's conutry can be described as a N*N matrix A,Aij indicates the city(i,j) have bidirectionals road to city(i+1,j) and city(i,j+1),gurad anyone of them costs Aij.
Now give you the map,help zjsxzy to calculate the minimium cost.We assume thieves may travel in any way,and we will catch all passing thieves on a road if we guard it.
 
Input
The first line is an integer T,followed by T test cases.
In each test case,the first line contains a number N(1<N<=400).
The following N lines,each line is N numbers,the jth number of the ith line is Aij.
The city A is always located on (1,1) and the city B is always located on (n,n).
Of course,the city (i,j) at the last row or last line won't have road to (i,j+1) or (i+1,j).
 
Output
For each case,print a line with a number indicating the minimium cost to arrest all thieves.
 
Sample Input
1 3 10 5 5 6 6 20 4 7 9
 
Sample Output
18 

几乎超时,1800+ms,之前TLE了无数次,貌似是因为head后面写了MAXM然后memset多次用的时间太多了

此题为求最小割,但点数太大,注意到是平面图,可以转化为最短路径模型,详细方法可以看周冬的论文《两极相通——浅析最大—最小定理在信息学竞赛中的应用》

 

SPFA代码如下

#include <cstdio>     
#include <queue>     
#include <cstring>     
#define INF 0x3fffffff     
#define MAXN 200000     
#define MAXM 1500010     
using namespace std;     
          
int d[MAXN] ,head[MAXN], to[MAXM], next[MAXM], cost[MAXM], ecnt;     
bool vis[MAXN];     
          
inline void addEdge(int u, int v, int w)     
{     
    next[ecnt]=head[u];head[u]=ecnt;     
    to[ecnt]=v;cost[ecnt++]=w;     
}     
          
void spfa(int S, int T){     
    for(int i = 0; i <= T; ++i) d[i] = INF;     
    queue<int> Q;     
    Q.push(S);     
    vis[S]=1;     
    d[S] = 0;     
    while(!Q.empty()){     
        int u = Q.front(); Q.pop();     
        vis[u] = 0;     
        for(int p = head[u]; p; p = next[p]){     
            if(d[to[p]] > d[u] + cost[p]){     
                d[to[p]] = d[u] + cost[p];     
                if(!vis[to[p]]){     
                    vis[to[p]]=1;     
                    Q.push(to[p]);     
                }     
            }     
        }     
    }     
    printf("%d\n",d[T]);     
}     
int main()     
{     
    int T, i, j, x, n, ss, tt;     
    scanf("%d",&T);     
    while(T--){     
        scanf("%d",&n);     
        ss = (n-1)*(n-1); tt = ss +1;     
        memset(head, 0, (tt+1)*sizeof(int));     
        ecnt = 1;     
        for(i = 0; i < n; ++i)     
            for(j = 0; j < n; ++j){     
                scanf("%d",&x);     
                if(j != n-1){     
                    if(i == 0) addEdge(ss,i*(n-1)+j,x);     
                    else if(i == n-1) addEdge((i-1)*(n-1)+j,tt,x);     
                    else {addEdge(i*(n-1)+j,(i-1)*(n-1)+j,x);addEdge((i-1)*(n-1)+j,i*(n-1)+j,x);}     
                }     
                if(i != n-1){     
                    if(j == 0) addEdge(i*(n-1)+j,tt,x);     
                    else if(j == n-1) addEdge(ss,i*(n-1)+j-1,x);     
                    else {addEdge(i*(n-1)+j,i*(n-1)+j-1,x);addEdge(i*(n-1)+j-1,i*(n-1)+j,x);}     
                }     
            }     
        spfa(ss, tt);     
    }     
    return 0;     
}
SPFA

相关文章: