Codeforces 852D
数目规模大,尝试转换为判定性问题,那么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的更多相关文章
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- CodeForces - 148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- 第二天:tomcat体系结构和第一个Servlet
1. 打war包 2. Tomcat体系再说明: 问题:如何去配置默认主机??? 3.tomcat和servlet在网络中的位置 4. servlet快速入门案例 1).开发s ...
- activity状态保存的bundl对象存放位置的思考
我们知道,当activity被异常终止时,可以把一些信息保存到bundle对象中,在下次启动时恢复. 那么,这个bundle对象是保存在哪里的呢? 这种状态保存的方法针对的是activity而不是进程 ...
- 关于mybatis和spring复合pom的异常
java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L ...
- PHP如何将XML转成数组
如果你使用 curl 获取的 xml data $xml = simplexml_load_string($data); $data['tk'] = json_decode(json_encode($ ...
- android sdk更新源
什么是Android SDK: SDK:(software development kit)软件开发工具包.被软件开发工程师用于为特定的软件包.软件框架.硬件平台.操作系统等建立应用软件的开发工具的集 ...
- sublime 配置 anaconda 环境
安装清单: 软件列表: anaconda sublime text sublime插件列表: package control Conda 安装 anaconda https://www.continu ...
- str() 和repr()的区别
>>> a='bbc' >>> a'bbc'>>> print abbc str()一般是将数值转成字符串:repr()是将一个对象转成字符串显示 ...
- 多用户ATM机(面向对象编程)
let readline = require('readline-sync'); // 清屏函数 let clear = () => process.stdout.write(process.p ...
- Failed to export using the options you specified. Please check your options and try again
参考这篇<从ASP.NET传递参数给水晶报表> http://www.cnblogs.com/insus/p/3281114.html 是可以传递参了.但是点击报表的菜单条上的打印图标没 ...
- javascript dom与字符串相互转换
js dom与字符串相互转换 一.字符串转换dom: function stringToDom(str){ var obj=document.createElement("div" ...