记录编号 |
394605 |
评测结果 |
AAAAAAAAAAAAAAAAAAAA |
题目名称 |
源符「厌川的翡翠」 |
最终得分 |
100 |
用户昵称 |
FoolMike |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
2.194 s |
提交时间 |
2017-04-13 19:19:13 |
内存使用 |
42.44 MiB |
显示代码纯文本
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int N=1e6+10,V=1010;
vector<int> Edge[V];
int n,m,W,T,id[V][V],v[V][V];
struct edge{int f,t,g;}w[N];
int s,t,next[N],head[N],cnt=1;
void add(int f,int t,int g){
w[++cnt]=(edge){f,t,g};
next[cnt]=head[f];
head[f]=cnt;
w[++cnt]=(edge){t,f,0};
next[cnt]=head[t];
head[t]=cnt;
}
struct st{
int x,i,df;
st(int X=0,int DF=0){x=X;i=head[x];df=DF;}
}z[N];
int l[N],top,ans;
#define V z[top].x
#define E z[top].i
#define F z[top].df
void change(){
int df=F;ans+=F;
for (int i=top-1;i;i--){
w[z[i].i].g-=df;
w[z[i].i^1].g+=df;
z[i].df-=df;
if (!z[i].df) top=i;
}
}
queue<int> Q;
void bfs(){
for (int i=s;i<=t;i++) l[i]=0;
l[s]=1;Q.push(s);
while (!Q.empty()){
int v=Q.front();Q.pop();
if (l[t]) continue;
for (int i=head[v];i;i=next[i])
if (w[i].g&&!l[w[i].t])
l[w[i].t]=l[v]+1,Q.push(w[i].t);
}
}
bool dinic(){
bfs();
if (!l[t]) return 0;
z[top=1]=st(s,1e9);
while (top){
if (V==t){change();top--;E=next[E];}else
if (!E){l[V]=0;top--;E=next[E];}else
if (w[E].g&&l[w[E].t]==l[V]+1)
z[top+1]=st(w[E].t,min(F,w[E].g)),top++;
else E=next[E];
}
return 1;
}
void init(int c){
while (cnt>1) next[cnt--]=0;
for (int i=s;i<=t;i++) head[i]=0;
for (int i=1;i<=n;i++)
for (int j=1;j<=T;j++)
add(id[i][j-1],id[i][j],v[i][j]);
for (int v=1;v<=n;v++)
for (int i=Edge[v].size()-1;i>=0;i--){
int u=Edge[v][i];
for (int j=c;j<=T;j++)
add(id[v][j],id[u][j-c],1e9);
}
ans=0;
}
void solve(int l,int r){
if (l==r) return (void)printf("%d\n",r<=T?r:-1);
int mid=(l+r)>>1;
init(mid);
while (dinic());
ans<=W?solve(l,mid):solve(mid+1,r);
}
int main()
{
freopen("cdcq_c.in","r",stdin);
freopen("cdcq_c.out","w",stdout);
scanf("%d%d%d%d",&n,&m,&W,&T);
for (int i=1;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
Edge[u].push_back(v);
Edge[v].push_back(u);
}
s=0;t=n*(T-1)+1;
int p=0;
for (int i=1;i<=n;i++)
for (int j=1;j<=T;j++){
scanf("%d",&v[i][j]);
id[i][j]=(j==T?t:++p);
}
solve(0,T+1);
return 0;
}