数目规模大,尝试转换为判定性问题,那么E就不影响到网络流的复杂度了

SPFA码歪了,WA了好几发气死了

#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+11;
const int oo = 0x3f3f3f3f;
int to[maxn<<1],nxt[maxn<<1],cap[maxn<<1],flow[maxn<<1];
int head[maxn],tot;
void init(){
memset(head,-1,sizeof head);
tot=0;
}
void add(int u,int v,int w){
to[tot]=v;
nxt[tot]=head[u];
cap[tot]=w;
flow[tot]=0;
head[u]=tot++;
swap(u,v);
to[tot]=v;
nxt[tot]=head[u];
cap[tot]=0;
flow[tot]=0;
head[u]=tot++;
}
int n,m,s,t;
int dis[maxn],pre[maxn],cur[maxn],gap[maxn];
bool vis[maxn];
struct QUEUE{
int que[maxn];
int front,rear;
void init(){front=rear=0;}
void push(int u){que[rear++]=u;}
int pop(){return que[front++];}
bool empty(){return front==rear;}
}que;
void bfs(){
memset(vis,0,sizeof vis);
que.init();
que.push(t);
vis[t]=1;dis[t]=0;
while(que.empty()^1){
int u = que.pop();
for(int i = head[u]; ~i; i = nxt[i]){
register int v=to[i],c=cap[i^1],f=flow[i^1];
if(!vis[v]&&c>f){
vis[v]=1;
dis[v]=dis[u]+1;
que.push(v);
}
}
}
}
int aug(){
int u=t,ans=oo;
while(u!=s){
ans=min(ans,cap[pre[u]]-flow[pre[u]]);
u=to[pre[u]^1];
}
u=t;
while(u!=s){
flow[pre[u]]+=ans;
flow[pre[u]^1]-=ans;
u=to[pre[u]^1];
}
return ans;
}
int isap(){
int ans=0;
bfs();
memset(gap,0,sizeof gap);
memcpy(cur,head,sizeof head);
for(int i = 1; i <= n; i++) gap[dis[i]]++;
int u = s;
while(dis[s]<n){
if(u==t){
ans+=aug();
u=s;
}
bool ok=0;
for(int i = cur[u]; ~i; i = nxt[i]){
int v=to[i],c=cap[i],f=flow[i];
if(c>f&&dis[u]==dis[v]+1){
ok=1;
pre[v]=i;
cur[u]=i;
u=v;
break;
}
}
if(!ok){
int mn=n-1;
for(int i = head[u]; ~i; i = nxt[i]){
int v=to[i],c=cap[i],f=flow[i];
if(c>f) mn=min(mn,dis[v]);
}
if(--gap[dis[u]]==0) break;
dis[u]=mn+1;gap[dis[u]]++;cur[u]=head[u];
if(u!=s) u=to[pre[u]^1];
}
}
return ans;
}
#define rep(i,j,k) for(int i = j; i <= k; i++)
#define repp(i,j,k) for(int i = j; i < k; i++)
#define repe(i,u) for(int i = head[u]; ~i; i = nxt[i])
#define scan(a) scanf("%d",&a)
#define scann(a,b) scanf("%d%d",&a,&b)
#define scannn(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define println(a) printf("%d\n",a)
#define printbk(a) printf("%d ",a)
#define print(a) printf("%d",a)
int V,E,N,K,st[maxn];//V city
long long G[605][605]; int cost[maxn<<1];
void init2(){
memset(head,-1,sizeof head);
tot=0;
}
long long dd[maxn];
bool in[maxn];
void add2(int u,int v,int w){
to[tot]=v;cost[tot]=w;nxt[tot]=head[u];head[u]=tot++;
swap(u,v);
to[tot]=v;cost[tot]=w;nxt[tot]=head[u];head[u]=tot++;
}
void spfa(int s){
memset(dd,oo,sizeof dd);memset(in,0,sizeof in);
queue<int> q;
q.push(s);in[s]=1;dd[s]=0;
while(!q.empty()){
int u=q.front();q.pop();in[u]=0;
for(int i = head[u]; ~i; i = nxt[i]){
int v=to[i],w=cost[i];
if(dd[v]>dd[u]+w){
dd[v]=dd[u]+w;
if(!in[v]){
in[v]=1;
q.push(v);
}
}
}
}
}
bool C(int mid){
init();
#define rg(x) ((x)+N)
s=N+V+1; t=s+1; n=t;
rep(i,1,N) add(s,i,1);
rep(i,1,V) add(rg(i),t,1);
rep(i,1,N){
rep(j,1,V){
if(G[st[i]][j]<=mid)
add(i,rg(j),1);
}
}
int ans=isap();//cout<<mid<<" "<<ans<<endl;
return ans>=K;
}
#define rr register
bool go[maxn];
int main(){
while(scann(V,E)!=EOF){
scann(N,K);memset(G,oo,sizeof G);init2();
rep(i,1,N) scan(st[i]);
rep(i,1,E){
rr int f,t,w;
scannn(f,t,w);
add2(f,t,w);
}
memset(go,0,sizeof go);
rep(i,1,N){
if(!go[st[i]]){
go[st[i]]=1;
spfa(st[i]);
}
else continue;
rep(j,1,V){
G[st[i]][j]=min(G[st[i]][j],dd[j]);
G[j][st[i]]=min(G[j][st[i]],dd[j]);
}
}
int l=0,r=1731311,mid;
if(!C(r)){
println(-1);
continue;
}
while(l<=r){
mid=l+(r-l)/2;
if(C(mid)) r=mid-1;
else l=mid+1;
}
println(l);
}
return 0;
}

Codeforces 852D的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

随机推荐

  1. python之特殊方法

    特殊方法的定义: 1.定义在某些class当中 2.不需要直接调用 3.Python的某些函数或者是操作符会调用相应的特殊方法 特殊方法很多,我们只需要编写用到的特殊方法,以及有关联性的特殊方法. — ...

  2. SpringMVC接收对象数组参数进行封装

    前台代码:注意.contentType : "application/json; charset=utf-8",必须要设置,只有这样SpringMVC才认识这个json数组参数 f ...

  3. winform 打印

    pageSetupDialog 打印设置,和对话框控件差不多的套路,把控件拖到窗口中后,会在下方显示, 然后在制作的菜单中找到打印设置,双击进入点击事件写代码 按照之前的套路, DialogResul ...

  4. c# 新中新二代身份证阅读,包含头像,支持华视

    需要用到dll和文件: 其中3个dll文件是需要调用的dll,license.dat文件为解压图片的授权文件 以下是需要用到的dll里面的方法: /************************端口 ...

  5. Luogu 3934 Nephren Ruq Insania

    和Ynoi2016 炸脖龙重题了. BZOJ 5394. 首先是扩展欧拉定理: 一开始傻掉了……递归的层数和区间长度无关……也就是说我们每一次直接暴力递归求解子问题一定不会超过$logP$层,因为当模 ...

  6. python中list的使用

    1.list(列表)是一种有序的集合,可以随时添加.修改.删除其中的元素. 举例:listClassName = ['Jack','Tom','Mark'] 列表可以根据索引获取元素,如:listCl ...

  7. SDUT 3364 数据结构实验之图论八:欧拉回路

    数据结构实验之图论八:欧拉回路 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 在哥尼斯堡的 ...

  8. 2015年第六届蓝桥杯省赛试题(JavaA组)

    1.结果填空 (满分3分)2.结果填空 (满分5分)3.结果填空 (满分9分)4.代码填空 (满分11分)5.代码填空 (满分13分)6.结果填空 (满分17分)7.结果填空 (满分21分)8.程序设 ...

  9. [转]oracle11g 修改字符集 修改为ZHS16GBK

    转至:http://www.cnblogs.com/jay-xu33/p/5210098.html sqlplus /nolog conn /as sysdba shutdown immediate; ...

  10. c# XML增删改查

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...