A - Perspective
Crawling in process... Crawling failed Time Limit:500MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
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 w 1, w 2,..., w N, where w i is the total number of games that i th team has won to the moment.
The third line of input contains N integers r 1, r 2,..., r N, where r i is the total number of remaining games for the i th team (including the games inside the division).
The next N lines contain N integers each. The j th integer in the i th line of those contains a ij — the number of games remaining between teams i and j. It is always true that a ij=a ji and a ii=0, for all ia i1 + a i2 +... + a iN ≤ r i.
All the numbers in input are non-negative and don't exceed 10\,000.
The second line of input contains N integers w 1, w 2,..., w N, where w i is the total number of games that i th team has won to the moment.
The third line of input contains N integers r 1, r 2,..., r N, where r i is the total number of remaining games for the i th team (including the games inside the division).
The next N lines contain N integers each. The j th integer in the i th line of those contains a ij — the number of games remaining between teams i and j. It is always true that a ij=a ji and a ii=0, for all ia i1 + a i2 +... + a iN ≤ r i.
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.
Sample Input
sample input | sample output |
3 1 2 2 1 1 1 0 0 0 0 0 0 0 0 0 | YES |
sample input | sample output |
3 1 2 2 1 1 1 0 0 0 0 0 1 0 1 0 | NO |
网络流
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
#define INF 100000001
const int maxint=1000001;
int n,m,s,t;
int num[100],remain[100];
int map[100][100];
struct edgee
{
int from,to,cap,flow;
};
vector<edgee> edges;
vector<int> G[10003];
bool vis[10003];
int dist[10003];
int cur[10003];
void add(int from,int to,int cap)
{
edges.push_back((edgee){from,to,cap,0});
edges.push_back((edgee){to,from,0,0});
int k=edges.size();
G[from].push_back(k-2);//奇数为正向弧
G[to].push_back(k-1);
}
int max(int a,int b)
{
return a>b?a:b;
}
int min(int a,int b)
{
return a<b?a:b;
}
bool bfs()
{
memset(vis,0,sizeof(vis));
queue<int> q;
dist[s]=0;
vis[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
int sz=G[u].size();
for(int i=0;i<sz;i++)
{
edgee& e=edges[G[u][i]];
if(!vis[e.to] && e.cap>e.flow)
{
vis[e.to]=1;
dist[e.to]=dist[u]+1;
q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int u,int low)
{
if(u==t || low==0)
return low;
int flow=0,sz=G[u].size(),d;//&引用的妙用
for(int& i=cur[u];i<sz;i++) //正在考虑的最后一条边开始考虑(效率关键)
{
edgee& e=edges[G[u][i]];
if(dist[u]+1==dist[e.to] && (d=dfs(e.to,min(e.cap-e.flow,low)))>0 )
{
e.flow+=d;
edges[G[u][i]^1].flow-=d;
flow+=d;
low-=d;
if(low==0)
break;
}
}
return flow;
}
int Dinic()
{
int flow=0;
while(bfs())
{
memset(cur,0,sizeof(cur));
flow+=dfs(s,maxint);
}
return flow;
}
int main()
{
int i,j;
s=4003,t=4004;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
for(int i=1;i<=n;i++)
scanf("%d",&remain[i]);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&map[i][j]);
num[1]+=remain[1];
for(int i=1;i<=n;i++)
if(map[1][i]!=0)
map[1][i]=map[i][1]=0;
for(int i=2;i<=n;i++)
{
add(i,t,INF);
if(num[1]-num[i]<0)
{
printf("NO\n");
return 0;
}
add(i+100,i,num[1]-num[i]);
}
int k=0;
int sum=0;
for(int i=2;i<=n;i++)
for(int j=2;j<=n;j++)
if(map[i][j]!=0)
{
k++;
sum+=map[i][j];
add(300+k,i+100,INF);
add(300+k,j+100,INF);
add(s,300+k,map[i][j]);
map[i][j]=map[j][i]=0;
}
int temp=Dinic();
if(temp==sum)
printf("YES\n");
else
printf("NO\n");
return 0;
}