sgu326 贪心+dinic

来源:互联网 发布:逆战一键瞬狙宏软件 编辑:程序博客网 时间:2024/05/14 02:51

326. Perspective

Time limit per test: 0.25 second(s)
Memory limit: 65536 kilobytes
input: standard
output: standard



Breaking news! A Russian billionaire has bought a yet undisclosed NBA team. He's planning to invest huge effort and money into making that team the best. And in fact he's been very specific about the expected result: the first place.

Being his advisor, you need to determine whether it's possible for your team to finish first in its division or not.

More formally, the NBA regular season is organized as follows: all teams play some games, in each game one team wins and one team loses. Teams are grouped into divisions, some games are between the teams in the same division, and some are between the teams in different divisions.

Given the current score and the total number of remaining games for each team of your division, and the number of remaining games between each pair of teams in your division, determine if it's possible for your team to score at least as much wins as any other team in your division.

Input
The first line of input contains N (2 ≤ N ≤ 20) — the number of teams in your division. They are numbered from 1 to N, your team has number 1.

The second line of input contains N integers w1w2,..., wN, where wi is the total number of games that ith team has won to the moment.

The third line of input contains N integers r1r2,..., rN, where ri is the total number of remaining games for the ith team (including the games inside the division).

The next N lines contain N integers each. The jth integer in the ith line of those contains aij — the number of games remaining between teams i and j. It is always true that aij=aji and aii=0, for all i ai1 + ai2 +... + aiN ≤ ri.

All the numbers in input are non-negative and don't exceed 10\,000.

Output
On the only line of output, print "
YES
" (without quotes) if it's possible for the team 1 to score at least as much wins as any other team of its division, and "
NO
" (without quotes) otherwise.

Example(s)
sample input
sample output
31 2 21 1 10 0 00 0 00 0 0
YES

sample input
sample output
31 2 21 1 10 0 00 0 10 1 0
NO


英语太渣,竟然看不懂题意...

题意:有两个赛区,每个赛区有n支队伍,输入本赛区n个队伍目前已获胜的比赛场数score[i]和还要进行的比赛场数num[i](包括和本赛区的其他队伍的比赛以及和另一个赛区n个队伍的比赛),接下来是一个本赛的比赛矩阵,Aij表示本赛区i队和j队还需要进行Aij场比赛,问是否存在一种方案1队为本赛区第一(可与本赛区其他队并列第一)。

此题与woj1124极其相似,传送门:woj1124

首先是贪心,一队赢得所有剩余比赛。

num[i]-sigma{j=[1...n]}(aij)就是i队与另一个赛区的比赛场数了,i队显然输掉这些比赛。

剩下来就是本赛区i队和j队的比赛(i,j均不为1),这里建模完全和woj1124类似,不再赘述。注意:Aij可以大于1.

代码:

#include<cstdio>#include<iostream>#include<cstring>#define Maxn 430using namespace std;int score[Maxn],num[Maxn],mat[Maxn][Maxn];const int inf=0x3f3f3f3f;struct line{    int to,next,cap;}p[Maxn*Maxn];int head[Maxn];int q[Maxn];int d[Maxn];int tot;int src,t;int n,m;void addedge(int a,int b,int c){    p[tot].to=b;    p[tot].next=head[a];    p[tot].cap=c;    head[a]=tot++;}void insert(int a,int b,int c){    addedge(a,b,c);    addedge(b,a,0);}bool bfs(){    memset(d,-1,sizeof d);    int s=0,e=-1;    q[++e]=src;    d[src]=0;    while(s<=e){        int u=q[s++];        for(int i=head[u];i!=-1;i=p[i].next){            int v=p[i].to;            if(d[v]==-1&&p[i].cap){                d[v]=d[u]+1;                q[++e]=v;            }        }    }    return d[t]!=-1;}int dfs(int u,int alpha){    if(u==t) return alpha;    int w,used=0;    for(int i=head[u];i!=-1&&used<alpha;i=p[i].next){        int v=p[i].to;        if(p[i].cap&&d[v]==d[u]+1){            w=dfs(v,min(alpha-used,p[i].cap));            used+=w;            p[i].cap-=w;            p[i^1].cap+=w;        }    }    if(!used) d[u]=-1;    return used;}int dinic(){    int ans=0;    src=0;    while(bfs())        ans+=dfs(src,inf);    return ans;}int main(){    while(cin>>n){        for(int i=1;i<=n;i++)            scanf("%d",score+i);        for(int i=1;i<=n;i++)            scanf("%d",num+i);        for(int i=1;i<=n;i++)            for(int j=1;j<=n;j++)                scanf("%d",&mat[i][j]);        score[1]+=num[1];        bool flag=true;        for(int i=2;i<=n;i++)            if(score[i]>score[1]) {flag=false;break;}        if(!flag) {puts("NO");continue;}        t=n+1;        int res=0;        tot=0;        memset(head,-1,sizeof head);        for(int i=2;i<=n;i++)            for(int j=i+1;j<=n;j++)                if(mat[i][j]>0){                    res+=mat[i][j];                    insert(0,t,mat[i][j]);                    insert(t,i,mat[i][j]);                    insert(t,j,mat[i][j]);                    t++;                }        for(int i=2;i<=n;i++)            insert(i,t,score[1]-score[i]);        if(res==dinic()) puts("YES");        else puts("NO");    }return 0;}


0 0
原创粉丝点击