uva 563Crimewave

来源:互联网 发布:win7网络连接感叹号 编辑:程序博客网 时间:2024/05/17 06:50

原题:
Nieuw Knollendam is a very modern town. This becomes clear already when looking at the layout of its map, which is just a rectangular grid of streets and avenues. Being an important trade centre, Nieuw Knollendam also has a lot of banks. Almost on every crossing a bank is found (although there are never two banks at the same crossing). Unfortunately this has attracted a lot of criminals. Bank hold-ups are quite common, and often on one day several banks are robbed. This has grown into a problem, not only to the banks, but to the criminals as well. After robbing a bank the robber tries to
leave the town as soon as possible, most of the times chased at high speed by the police. Sometimes two running criminals pass the same crossing, causing several risks: collisions, crowds of police at one place and a larger risk to be caught. To prevent these unpleasant situations the robbers agreed to consult together. Every Saturday night they meet and make a schedule for the week to come: who is going to rob which bank on which day? For every day they try to plan the get-away routes, such that no two routes use the same crossing. Sometimes they do not succeed in planning the routes according to this condition, although they believe that such a planning should exist.
Given a grid of (s×a) and the crossings where the banks to be robbed are located, find out whether or not it is possible to plan a get-away route from every robbed bank to the city-bounds, without using a crossing more than once.
Input
The first line of the input contains the number of problems p to be solved.
• The first line of every problem contains the number s of streets (1 ≤ s ≤ 50), followed by the number a of avenues (1 ≤ a ≤ 50), followed by the number b (b ≥ 1) of banks to be robbed.
• Then b lines follow, each containing the location of a bank in the form of two numbers x (the number of the street) and y (the number of the avenue). Evidently 1 ≤ x ≤ s and 1 ≤ y ≤ a.
Output
The output file consists of p lines. Each line contains the text ‘possible’ or ‘not possible’. If it is possible to plan non-crossing get-away routes, this line should contain the word: ‘possible’. If this is not possible, the line should contain the words ‘not possible’.
Note: The picture on the right illustrates the first sample input below.
Sample Input
2
6 6 10
4 1
3 2
4 2
5 2
3 4
4 4
5 4
3 6
4 6
5 6
5 5 5
3 2
2 3
3 3
4 3
3 4
Sample Output
possible
not possible

中文:
给你一个s×a的二维网格表示街和路,然后给你b个坐标点,表示路口,一群劫匪要在这些路口的银行抢劫。现在这些劫匪要在这些路口跑路,如果走出这个s×a的网格就算逃跑成功。现在问你这些人能不能全跑出去,要求逃跑路线不能交叉!

#include <bits/stdc++.h>using namespace std;const int maxn=5501;struct Edge{    int from,to,cap,flow;    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f) {}};int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};struct EdmondsKarp{    int n,m;    vector<Edge> edges;    vector<int> G[maxn];    int a[maxn];    int p[maxn];    void init(int n)    {        for(int i=0;i<=n;i++)            G[i].clear();        edges.clear();    }    void AddEdge(int from,int to,int cap)    {        edges.push_back(Edge(from,to,cap,0));        edges.push_back(Edge(to,from,0,0));        m=edges.size();        G[from].push_back(m-2);        G[to].push_back(m-1);    }    int Maxflow(int s,int t)    {        int flow=0;        while(true)        {            memset(a,0,sizeof(a));            queue<int> Q;            Q.push(s);            a[s]=INT_MAX;            while(!Q.empty())            {                int x=Q.front();                Q.pop();                for(int i=0;i<G[x].size();i++)                {                    Edge& e = edges[G[x][i]];                    if(!a[e.to]&&e.cap>e.flow)                    {                        p[e.to]=G[x][i];                        a[e.to]=min(a[x],e.cap-e.flow);                        Q.push(e.to);                    }                }                if(a[t])                    break;            }            if(!a[t])                break;            for(int u=t;u!=s;u=edges[p[u]].from)            {                edges[p[u]].flow+=a[t];                edges[p[u]^1].flow-=a[t];            }            flow+=a[t];        }        return flow;    }};EdmondsKarp EK;int main(){    ios::sync_with_stdio(false);    int t;    cin>>t;    int s,a,b;    while(t--)    {        cin>>s>>a>>b;        EK.init(5500);        for(int i=1;i<=s;i++)        {            for(int j=1;j<=a;j++)            {                if(1==i||1==j||s==i||a==j)                    EK.AddEdge((i-1)*a+j+2500,5500,1);                EK.AddEdge((i-1)*a+j,(i-1)*a+j+2500,1);                for(int k=0;k<4;k++)                {                    int x=i+dir[k][0],y=j+dir[k][1];                    if(x<=0||x>s||y<=0||y>a)                        continue;                    EK.AddEdge((i-1)*a+j+2500,(x-1)*a+y,1);                }            }        }        for(int i=1;i<=b;i++)        {            int x,y;            cin>>x>>y;            EK.AddEdge(0,(x-1)*a+y,1);        }        int ans=EK.Maxflow(0,5500);       // cout<<ans<<endl;        if(ans==b)            cout<<"possible"<<endl;        else            cout<<"not possible"<<endl;    }    return 0;}

解答:

可以把这个问题的模型转换成最大流模型,建立源点连接抢劫的路口,建立汇点连接到整个网格图形的边界上。然后求最大流,如果最大流等于抢劫犯的数量,那就possible否则not possible
注意,由于网格中的每个点只能走一次,所以需要把这个点拆成两个点,使得只有这两个点的容量为1。

0 0
原创粉丝点击