HDU 3338 Kakuro Extension

建了两次图。第一次建图后样例都跑不对。

一气之下全删重新建图就对了。。

建图方式:

超级源点->行  权值=行和-行约束点个数

行->约束点      权值=8

约束点->列      权值=8

列->超级汇点   权值=列和-列约束点个数


//author: CHC
//First Edit Time:	2014-09-05 16:45
//Last Edit Time:	2014-09-05 17:20
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits.h>
using namespace std;
typedef long long LL;
const int MAXN=1e+5;
const int MAXM=1e+6;
const int INF = INT_MAX;
struct Edge
{
    int from,to,ci,next;
    Edge(){}
    Edge(int _from,int _to,int _ci,int _next):from(_from),to(_to),ci(_ci),next(_next){}
}e[MAXM];
int head[MAXN],tot;
int dis[MAXN];
int top,sta[MAXN],cur[MAXN];
inline void init(){
    memset(head,-1,sizeof(head));
    tot=0;
}
inline void AddEdge(int u,int v,int ci0,int ci1=0){
    e[tot]=Edge(u,v,ci0,head[u]);
    head[u]=tot++;
    e[tot]=Edge(v,u,ci1,head[v]);
    head[v]=tot++;
}
inline bool bfs(int st,int et){
    memset(dis,0,sizeof(dis));
    dis[st]=1;
    queue <int> q;
    q.push(st);
    while(!q.empty()){
        int now=q.front();
        q.pop();
        for(int i=head[now];i!=-1;i=e[i].next){
            int next=e[i].to;
            if(e[i].ci&&!dis[next]){
                dis[next]=dis[now]+1;
                if(next==et)return true;
                q.push(next);
            }
        }
    }
    return false;
}
LL Dinic(int st,int et){
    LL ans=0;
    while(bfs(st,et)){
        //printf("here\n");
        top=0;
        memcpy(cur,head,sizeof(head));
        int u=st,i;
        while(1){
            if(u==et){
                int pos,minn=INF;
                //printf("top:%d\n",top);
                for(i=0;i<top;i++)
                {
                    if(minn>e[sta[i]].ci){
                        minn=e[sta[i]].ci;
                        pos=i;
                    }
                    //printf("%d --> %d\n",e[sta[i]].from,e[sta[i]].to);
                }
                for(i=0;i<top;i++){
                    e[sta[i]].ci-=minn;
                    e[sta[i]^1].ci+=minn;
                }
                top=pos;
                u=e[sta[top]].from;
                ans+=minn;
                //printf("minn:%d\n\n",minn);
            }
            for(i=cur[u];i!=-1;cur[u]=i=e[i].next)
                if(e[i].ci&&dis[u]+1==dis[e[i].to])break;
            if(cur[u]!=-1){
                sta[top++]=cur[u];
                u=e[cur[u]].to;
            }
            else {
                if(top==0)break;
                dis[u]=0;
                u=e[sta[--top]].from;
            }
        }
    }
    return ans;
}
int n,m;
struct some
{
    int i,j,v;
    some(){}
    some(int _i,int _j,int _v):i(_i),j(_j),v(_v){}
}col[120*120],row[120*120];
int map1[120][120];
char tmp[100];
int main()
{
    while(~scanf("%d%d",&n,&m)){
        int n_col=0,n_row=0,n_emt=0;
        memset(map1,0,sizeof(map1));
        init();
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            scanf(" %s",tmp);
            if(strcmp(tmp,".......")==0){
                map1[i][j]=++n_emt;
            }
            else if(tmp[3]=='\\'){
                int x;
                if(tmp[0]!='X'){
                    //sscanf(tmp,"%d\\%*3s",&x);
                    x=(tmp[0]-'0')*100+(tmp[1]-'0')*10+(tmp[2]-'0');
                    col[++n_col]=some(i,j,x);
                }
                if(tmp[4]!='X'){
                    //sscanf(tmp,"%*3s\\%d",&x);
                    x=(tmp[4]-'0')*100+(tmp[5]-'0')*10+(tmp[6]-'0');
                    row[++n_row]=some(i,j,x);
                }
            }
        }
        for(int i=1;i<=n_row;i++){
            int x=row[i].i;
            int cnt=0;
            for(int y=row[i].j+1;y<=m;y++)
                if(map1[x][y]){
                    ++cnt;
                    AddEdge(i,n_row+map1[x][y],8);
                }
                else break;
            AddEdge(0,i,row[i].v-cnt);
        }
        for(int i=1;i<=n_col;i++){
            int y=col[i].j;
            int cnt=0;
            for(int x=col[i].i+1;x<=n;x++)
                if(map1[x][y]){
                    ++cnt;
                    AddEdge(n_row+map1[x][y],n_row+n_emt+i,8);
                }
                else break;
            AddEdge(n_row+n_emt+i,12000,col[i].v-cnt);
        }
        Dinic(0,12000);
        for(int i=1;i<=n;i++,puts(""))
        for(int j=1;j<=m;j++)
            if(map1[i][j]){
                int ans=0;
                for(int k=head[n_row+map1[i][j]];k!=-1;k=e[k].next){
                    if(e[k].to<=n_row)ans+=e[k].ci;
                }
                printf("%d ",ans+1);
            }
            else printf("_ ");
    }
    return 0;
}
/*
3 3
XXXXXXX 008\XXX 010\XXX
XXX\015 ....... .......
XXX\003 ....... .......
*/


你可能感兴趣的:(网络流)