HDU 4183 最大流模板题

Pahom on Water

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


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
 

Source
 

题意

有多个点,每个点给出坐标与半径,加入两个点相交,就可以从这两个点走。题目要求先从起点到终点,再从终点回到起点。从起点到终点的过程中,只能从频率小的走到频率大的点(前提是两点相交),从终点到起点的过程中,只能从频率大的走到频率小的。在走的过程中,除了起点与终点,别的只要走过就会消失,就是说只能走一次。问可不可以从起点到终点又回到起点。
无非就是从起点到终点走两次,均是从小到大,而且中间经过的点不重复即可。然后建图就很简单了。为了保证每个点只走一次,可以把权值设为1,这样每一步最多只能走一次。然后看最大流是否大于等于2即可。还有一点需要注意的是,如果可以直接从起点到终点的话,就不用判断了,肯定可以满足要求。
这题代码可以作为最大流模板~上代码咯


#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

const int maxn = 305;
const int inf = 0x3f3f3f3f;
struct Node {
    double f;
    int x,y,r;
} p[maxn];
struct Node1 {
    int v,w,next;
    Node1() {}
    Node1(int vv,int ww,int next1) {
        v=vv,w=ww,next=next1;
    }
} edge[maxn*maxn];
int n,cnt,head[maxn],lev[maxn],cur[maxn];
void init() {
    cnt=0;
    memset(head,-1,sizeof(head));
}
int s,t;
bool cmp(Node a,Node b)
{
    return a.f < b.f;
}

int dist(Node a,Node b)
{
    return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}

void addedge(int u,int v,int w) {
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].v=u;
    edge[cnt].w=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}

bool BFS() {
    memset(lev,0,sizeof(lev));
    lev[s]=1;
    queue<int>q;
    q.push(s);
    while(!q.empty()) {
        int u=q.front();
        q.pop();
        for(int i=head[u]; i!=-1; i=edge[i].next) {
            int v=edge[i].v;
            if(!lev[v]&&edge[i].w) {
                lev[v]=lev[u]+1;
                if(v==t)return  true;
                q.push(v);
            }
        }
    }
    return false;
}

int DFS(int u,int Maxflow) {
    if(u==t||Maxflow==0)return Maxflow;
    int sumf=0;
    for(int i=cur[u]; i!=-1; i=edge[i].next) {
        cur[u]=i;
        int v=edge[i].v;
        if(lev[v]==lev[u]+1&&edge[i].w) {
            int f=DFS(v,min(Maxflow,edge[i].w));
            sumf+=f,Maxflow-=f;
            edge[i].w-=f;
            edge[i^1].w+=f;
        }
    }
    if(!sumf)lev[u]=-1;
    return sumf;
}

int Dinic() {
    int ans=0;
    while(BFS()) {
        memcpy(cur,head,sizeof(head));
        ans+=DFS(s,inf);
    }
    return ans;
}

int main() {
    int T;
    scanf("%d",&T);
    while(T--) {
        scanf("%d",&n);
        for(int i = 1; i <= n; i++)
            scanf("%lf%d%d%d",&p[i].f,&p[i].x,&p[i].y,&p[i].r);
        memset(head,-1,sizeof(head));
        cnt = 0;
        s=1,t=n;
        sort(p+1,p+1+n,cmp);
        for(int i = 1; i < n; i++)
            for(int j = i + 1; j <= n; j++) {
                if(dist(p[i],p[j]) < (p[i].r + p[j].r) * (p[i].r + p[j].r))
                    addedge(i,j,1);
            }
        if(dist(p[1],p[n]) < (p[1].r + p[n].r) * (p[1].r + p[n].r))
            printf("Game is VALID\n");
        else {
            int ans = Dinic();
            if(ans >= 2)
                printf("Game is VALID\n");
            else printf("Game is NOT VALID\n");
        }
    }
    return 0;
}

你永远都不够优秀!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值