数目规模大,尝试转换为判定性问题,那么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. JavaScript Array.map

    Array.prototype.map() History Edit This article is in need of a technical review. Table of Contents ...

  2. day69-oracle 21-PLSQL课后作业

  3. 数据存储的两种方式:Cookie 和Web Storage(转)

    数据存储的两种方式:Cookie 和Web Storage   数据存储的两种方式:Cookie 和Web Storage 1.Cookie Cookie的作用就像你去超市购物时,第一次给你办张购物卡 ...

  4. 多线程 wait(),notify()方法,案例总结

    废话不多说,案例如下 package com.xujingyang.Exok; /** * 商品类 * @author 徐景洋 */ public class Goods { private Stri ...

  5. session和cookie个字消除的方法

    session消除的方法就是: session_destroy(); cookie消除的方法就是setcookie()函数的时间设为当前时间即可 if(isset($_COOKIE['adminId' ...

  6. docker创建容器打开两个端口

     docker run -d -it --name c6_3 -v :/mnt -p 5000:8000 -p 3000  centos  注释: -v 后面为共享文件夹

  7. IO流框架关系总结(关系图)

    字节流和字符流关系图  打印流和序列化流关系图

  8. UIActionSheet(操作列表)

    #import "AppDelegate.h" @interface AppDelegate ()<UIActionSheetDelegate> @end @imple ...

  9. Luogu 3302 [SDOI2013]森林

    BZOJ 3123 丑陋的常数,BZOJ 19968ms 卡过. 感觉几个思想都挺经典但是挺套路的. 先考虑不连边的情况怎么做,区间第$k$小,想到主席树,每一个结点维护它到根的前缀和,这样子每一次查 ...

  10. Redis面试考点

    什么是Redis? Redis 是一个基于内存的高性能key-value数据库. Redis的特点以及缺点? Redis本质上是一个Key-Value类型的内存数据库,很像memcached,整个数据 ...