HDU_4183_Pahom on Water(最大流)

Pahom on Water

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 729    Accepted Submission(s): 333



Problem Description
Pahom on Water is an interactive computer game inspired by a short story of Leo Tolstoy about a poor man who, in his lust for land, forfeits everything. The game's starting screen displays a number of circular pads painted with colours from the visible light spectrum. More than one pad may be painted with the same colour (defined by a certain frequency) except for the two colours red and violet. The display contains only one red pad (the lowest frequency of 400 THz) and one violet pad (the highest frequency of 789 THz). A pad may intersect, or even contain another pad with a different colour but never merely touch its boundary. The display also shows a figure representing Pahom standing on the red pad.
The game's objective is to walk the figure of Pahom from the red pad to the violet pad and return back to the red pad. The walk must observe the following rules:
1.If pad α and pad β have a common intersection and the frequency of the colour of pad α is strictly smaller than the frequency of the colour of pad β, then Pahom figure can walk from α to β during the walk from the red pad to the violet pad
2. If pad α and pad β have a common intersection and the frequency of the colour of pad α is strictly greater than the frequency of the colour of pad β, then Pahom figure can walk from α to β during the walk from the violet pad to the red pad
3. A coloured pad, with the exception of the red pad, disappears from display when the Pahom figure walks away from it.
The developer of the game has programmed all the whizzbang features of the game. All that is left is to ensure that Pahom has a chance to succeed in each instance of the game (that is, there is at least one valid walk from the red pad to the violet pad and then back again to the red pad.) Your task is to write a program to check whether at least one valid path exists in each instance of the game.
 

Input
The input starts with an integer K (1 <= K <= 50) indicating the number of scenarios on a line by itself. The description for each scenario starts with an integer N (2 <= N <= 300) indicating the number of pads, on a line by itself, followed by N lines that describe the colors, locations and sizes of the N pads. Each line contains the frequency, followed by the x- and y-coordinates of the pad's center and then the radius. The frequency is given as a real value with no more than three decimal places. The coordinates and radius are given, in meters, as integers. All values are separated by a single space. All integer values are in the range of -10,000 to 10,000 inclusive. In each scenario, all frequencies are in the range of 400.0 to 789.0 inclusive. Exactly one pad will have a frequency of “400.0” and exactly one pad will have a frequency of “789.0”.
 

Output
The output for each scenario consists of a single line that contains: Game is VALID, or Game is NOT VALID
 

Sample Input
  
  
2 2 400.0 0 0 4 789.0 7 0 2 4 400.0 0 0 4 789.0 7 0 2 500.35 5 0 2 500.32 5 0 3
 

Sample Output
  
  
Game is NOT VALID Game is VALID
 

题意:有N个点,给出每个点的频率、坐标与半径。两点相交即连通。起点是频率最小的那个点,终点是频率最大的那个点。题目要求先从起点到终点,再从终点回到起点。从起点到终点的过程中,只能从频率小的走到频率大的点(前提是两点相交),从终点到起点的过程中,只能从频率大的走到频率小的。在走的过程中,除了起点与终点,别的只要走过就会消失,就是说只能走一次。问可不可以从起点到终点又回到起点。

分析:最大流。题目相当于从起点走到终点走两次,但是两次走的路径都不相同。就转化成了最大流模型了。建好图后,如果最大流大于等于2的话,就可行。

建图:根据题意,先对频率进行sort,只能从小频率的点走到大频率的点,然后再判断是否相交,若相交则连一条容量为1的边。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4183

代码清单:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<cctype>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;

#define end() return 0

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

const int maxn = 300 + 5;
const int INF = 0x7f7f7f7f;

struct Edge{
    int from,to,cap,flow;
    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};

struct dinic{
    int n,m,s,t; //结点数,边数(包括反向弧),源点,汇点
    vector<Edge>edge;//边表。edge[e]和edge[e^1]互为反向弧
    vector<int>G[maxn];//邻接表。G[i][j]表示结点i的第j条边在e数组的序号
    bool vis[maxn]; //bfs用
    int d[maxn]; //从起点到i的距离
    int cur[maxn]; //当前弧下标

    void init(int n,int s,int t){
        this -> n = n;
        this -> s = s;
        this -> t = t;
        for(int i=0;i<=n;i++) G[i].clear();
        edge.clear();
    }

    void addEdge(int from,int to,int cap){
        edge.push_back(Edge(from,to,cap,0));
        edge.push_back(Edge(to,from,0,0));
        m=edge.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool bfs(){
        memset(vis,false,sizeof(vis));
        queue<int>q;
        q.push(s);
        d[s]=0;
        vis[s]=true;
        while(!q.empty()){
            int x=q.front();q.pop();
            for(int i=0;i<G[x].size();i++){
                Edge& e=edge[G[x][i]];
                if(!vis[e.to]&&e.cap>e.flow){ //只考虑残量网络中的弧
                    vis[e.to]=true;
                    d[e.to]=d[x]+1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int dfs(int x,int a){
        if(x==t||a==0) return a;
        int flow=0,f;
        for(int& i=cur[x];i<G[x].size();i++){ // & -> 从上次考虑的弧
            Edge& e=edge[G[x][i]];
            if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0){
                e.flow+=f;
                edge[G[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(a==0) break;
            }
        }
        return flow;
    }

    int maxflow(){
        int flow=0;
        while(bfs()){
            memset(cur,0,sizeof(cur));
            flow+=dfs(s,INF);
            if(flow>=2) break;
        }
        return flow;
    }
};

int T;
int N;
struct Node{
    double color;
    double x,y,r;
}node[305];
dinic dc;

bool cmp(Node a,Node b){
    return a.color<b.color;
}

void input(){
    scanf("%d",&N);
    for(int i=0;i<N;i++){
        scanf("%lf%lf%lf%lf",&node[i].color,&node[i].x,&node[i].y,&node[i].r);
    }
}

double Dis(Node a,Node b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

bool judge(Node a,Node b){
    if(Dis(a,b)<a.r+b.r) return true;
    return false;
}

void createGraph(){
    dc.init(N,0,N-1);
    for(int i=0;i<N-1;i++){
        for(int j=i+1;j<N;j++){
            if(judge(node[i],node[j]))
                dc.addEdge(i,j,1);
        }
    }
}

void solve(){
    sort(node,node+N,cmp);
    createGraph();
    if(dc.maxflow()<2) puts("Game is NOT VALID");
    else puts("Game is VALID");
}

int main(){
    scanf("%d",&T);
    while(T--){
        input();
        solve();
    }end();
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值