hdu3468 最大流/二分匹配+BFS

Treasure Hunting

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 903    Accepted Submission(s): 228


Problem Description
Do you like treasure hunting? Today, with one of his friend, iSea is on a venture trip again. As most movie said, they find so many gold hiding in their trip.
Now iSea’s clever friend has already got the map of the place they are going to hunt, simplify the map, there are three ground types:

● '.' means blank ground, they can get through it
● '#' means block, they can’t get through it
● '*' means gold hiding under ground, also they can just get through it (but you won’t, right?)

What makes iSea very delighted is the friend with him is extraordinary justice, he would not take away things which doesn’t belong to him, so all the treasure belong to iSea oneself! 
But his friend has a request, he will set up a number of rally points on the map, namely 'A', 'B' ... 'Z', 'a', 'b' ... 'z' (in that order, but may be less than 52), they start in 'A', each time friend reaches to the next rally point in the shortest way, they have to meet here (i.e. iSea reaches there earlier than or same as his friend), then start together, but you can choose different paths. Initially, iSea’s speed is the same with his friend, but to grab treasures, he save one time unit among each part of road, he use the only one unit to get a treasure, after being picked, the treasure’s point change into blank ground.
Under the premise of his friend’s rule, how much treasure iSea can get at most?

 

Input
There are several test cases in the input.

Each test case begin with two integers R, C (2 ≤ R, C ≤ 100), indicating the row number and the column number.
Then R strings follow, each string has C characters (must be ‘A’ – ‘Z’ or ‘a’ – ‘z’ or ‘.’ or ‘#’ or ‘*’), indicating the type in the coordinate.

The input terminates by end of file marker.
 

Output
For each test case, output one integer, indicating maximum gold number iSea can get, if they can’t meet at one or more rally points, just output -1.

 

Sample Input
  
  
2 4 A.B. ***C 2 4 A#B. ***C
 

Sample Output
  
  
1 2
 

Author
iSea @ WHU
 

Source
 

Recommend
zhouzeyong
 
参考了别人的思路~~
网络流建图是难点啊!!
#include <cstring>
#include <cstdio>
#include <queue>
#include <cstdlib>
#include <cctype>
#define MAXN 105*105*2
#define MAXM 105*105*100
#define inf 0x3f3f3f3f
using namespace std;
struct node
{
    int u,v,f;
};
node e[MAXM];
int first[MAXN],next[MAXM];
int gap[MAXN],d[MAXN],curedge[MAXN],pre[MAXN];
int cc, n,m;
inline void add_edge(int u,int v,int f)
{
    e[cc].u=u;
    e[cc].v=v;
    e[cc].f=f;
    next[cc]=first[u];
    first[u]=cc;
    cc++;

    e[cc].u=v;
    e[cc].v=u;
    e[cc].f=0;
    next[cc]=first[v];
    first[v]=cc;
    cc++;

}
int ISAP(int s,int t,int n)
{
    int cur_flow,flow_ans=0,u,tmp,neck,i,v;
    memset(d,0,sizeof(d));
    memset(gap,0,sizeof(gap));
    memset(pre,-1,sizeof(pre));
    for(i=0;i<=n;i++)
        curedge[i]=first[i];
    gap[0]=n+1;
    u=s;
    while(d[s]<=n)
    {
        if(u==t)
        {
            cur_flow=inf;
            for(i=s;i!=t;i=e[curedge[i]].v)
            {
                if(cur_flow>e[curedge[i]].f)
                {
                    neck=i;
                    cur_flow=e[curedge[i]].f;
                }
            }
            for(i=s;i!=t;i=e[curedge[i]].v)
            {
                tmp=curedge[i];
                e[tmp].f-=cur_flow;
                e[tmp^1].f+=cur_flow;
            }
            flow_ans+=cur_flow;
            u=neck;
        }
        for(i=curedge[u];i!=-1;i=next[i])
        {
            v=e[i].v;
            if(e[i].f&&d[u]==d[v]+1)
                break;
        }
        if(i!=-1)
        {
            curedge[u]=i;
            pre[v]=u;
            u=v;
        }
        else
        {
            if(0==--gap[d[u]])
                break;
            curedge[u]=first[u];
            for(tmp=n+5,i=first[u];i!=-1;i=next[i])
                if(e[i].f)
                    tmp=min(tmp,d[e[i].v]);
            d[u]=tmp+1;
            ++gap[d[u]];
            if(u!=s)
                u=pre[u];
        }
    }
    return flow_ans;
}
char map[105][105];
int rally[105*105];
int gold[105*105];
int pos[105*105];
int dist[55][105*105];
int vis[105][105];
int xx[4]={0,1,0,-1};
int yy[4]={1,0,-1,0};
int trans(char word)
{
    if(word>='A'&&word<='Z')
        return word-'A';
    else
        return word-'a'+26;
}
void bfs(int s)
{
    queue<int> q;
    memset(vis,0,sizeof(vis));
    memset(dist[s],inf,sizeof(dist[s]));
    int t=pos[s];
    vis[t/m][t%m]=1;
    q.push(t);
    dist[s][t]=0;
    while(!q.empty())
    {

        int u=q.front();
        q.pop();
        int x=u/m;
        int y=u%m;
        int i;
        for(i=0;i<4;i++)
        {
            int nx=x+xx[i];
            int ny=y+yy[i];
            if(nx<0||nx>=n||ny<0||ny>=m)
                continue;
            if(vis[nx][ny]||map[nx][ny]=='#')
                continue;
            vis[nx][ny]=1;
            dist[s][nx*m+ny]=dist[s][x*m+y]+1;
            q.push(nx*m+ny);
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(first,-1,sizeof(first));
        memset(next,-1,sizeof(next));
        cc=0;
        memset(pos,0,sizeof(pos));
        int i,j;
        int cnt1=0;
        int cnt2=0;

        for(i=0;i<n;i++)
        {
            scanf("%s",map[i]);
            for(j=0;j<m;j++)
            {
                if(isalpha(map[i][j]))
                {
                    pos[trans(map[i][j])]=i*m+j;
                    cnt1++;
                }
                else if(map[i][j]=='*')
                {
                    gold[cnt2]=i*m+j;
                    cnt2++;
                }
            }
        }
        for(i=0;i<cnt1;i++)
            bfs(i);
        int flag=1;
        for(i=1;i<=cnt1-1;i++)
        {
            for(j=0;j<cnt2;j++)
            {
                if(dist[i][gold[j]]+dist[i-1][gold[j]]==dist[i-1][pos[i]])
                {
                    add_edge(i,cnt1+j,1);
                }
                if(dist[i-1][pos[i]]==inf)
                {
                    flag=0;
                }
            }
        }
        if(!flag)
        {
            printf("-1\n");
            continue;
        }
        int s=0,t=cnt1+cnt2;
        for(i=1;i<=cnt1-1;i++)
            add_edge(s,i,1);
        for(i=0;i<cnt2;i++)
            add_edge(cnt1+i,t,1);
        int res=ISAP(s,t,t);
        printf("%d\n",res);

    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值