数目规模大,尝试转换为判定性问题,那么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. Ubuntu安装Chrome及hosts修改

    Ubuntu16.04 1.chrome安装 获取安装包http://www.google.cn/chrome/browser/desktop/index.html 在安装包目录打开终端执行sudo  ...

  2. Bytes和bits的区别(字节和位的区别)

    基本概念 Bit意为“位”或“比特”,是计算机运算的基础,属于二进制的范畴: Byte意为“字节”,是计算机文件大小的基本计算单位: 这两者应用的场合不同.通常用bit来作数据传输的单位,因为物理层, ...

  3. Oracle pl/sql 显示游标和隐式游标

    显示游标 一.定义语法:        CURSOR <游标名> IS         <SELECT 语句>         [FOR UPDATE | FOR UPDATE ...

  4. nodejs的POST请求

    http://blog.csdn.net/puncha/article/details/9015317 Nodejs 发送HTTP POST请求实例 2013-06-03 17:55 71745人阅读 ...

  5. 把数据分配到view

  6. 23种计模式之Python实现(史上最全最通俗易懂)内容整改中

    第一篇 Python与设计模式:前言 第二篇(23种设计模式) 创建类设计模式(5种) 单例模式.工厂模式.简单工厂模式.抽象工厂模式.建造者模式.原型模式 结构类设计模式(7种) 代理模式.装饰器模 ...

  7. 笔录---果壳中的C#第一章

    ---恢复内容开始--- 笔录---果壳中的C#第二章 2.1 第一个C#程序 1.C#语句按顺序执行,以“:”结尾. Console.WriteLine();     console 为类,Writ ...

  8. meteor 命令文件shell 解析

    #!/bin/bash # This is the script that we install somewhere in your $PATH (as "meteor")# wh ...

  9. git命令(二)

    2.查看.添加.提交.删除.找回,重置修改文件 3.查看文件diff git diff <file> # 比较当前文件和暂存区文件差异 git diff git diff <$id1 ...

  10. c/c++ 字符编码与标识符

    说明: 对于比较现代的语言来讲字符编码不是个大问题:java就可以使用中文作为变量名称,但对于C/C++来讲却不是这样,由于历史原因,标准和编译器厂商的实现总在不停的变化,相关编码信息到底是如何处理的 ...