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只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- FileZilla Server下通过别名设置虚拟目录
说明:FileZilla Server 的虚拟目录设置与其它 FTP 服务器软件有所不同.在 FileZilla Server 中设置虚拟目录,必须采用 FTP 根目录 + 虚拟目录名的形式来进行.比 ...
- solr通过http请求搜索
请求搜索必要的条件是:设置搜索条件params 设置 1.简单条件 SolrParams params = new SolrQuery("name:小飞鸟 AND id:1520" ...
- Ros学习——C++发布器publisher和订阅器subscriber
1.编写发布器 初始化 ROS 系统 在 ROS 网络内广播我们将要在 chatter 话题上发布 std_msgs/String 类型的消息 以每秒 10 次的频率在 chatter 上发布消息 在 ...
- Ros学习——roslaunch
roslaunch:启动定义在launch文件中的多个节点 1.launch文件解析 <launch> #以launch标签开头以表明这是一个launch文件 #两个节点分组并以'命名空间 ...
- 算法Sedgewick第四版-第1章基础-2.3 Quicksort-001快速排序
一. 1.特点 (1)The quicksort algorithm’s desirable features are that it is in-place (uses only a small a ...
- python常用uuid模块
uuid.uuid4(),会根据我们当前的网卡和时间生成的一个随机字符串. 注意:uuid.uuid4()生成的是一个对象,需要强转为字符串. uid = str(uuid.uuid4()) #当前网 ...
- springcloud +spring security多种验证方式之第三方token生成自己的token通过校验和自己的表单验证大体流程
步骤: 1.继承 WebSecurityConfigurerAdapter.class,其中使用两个过滤器,一个spring scurity自带的UsernamePasswordAuthenticat ...
- centos7.4版本安装nmon监控软件
一.检查安装环境 # uname –a (查看操作系统信息,所检查服务器为64位操作系统) Linux iZ94pmb2p24Z 2.6.32-431.23.3.el6.x86_64 #1 SMP T ...
- mybatis新增对象自动生成uuid方案
mybatis新增对象时, 使用拦截器自动生成uuid方案有了它不再去xml中手动添加selectKey了, 生成id方案实现类自行实现, 实现IdGenerator接口便可, 主要代码由公司同事编写 ...
- fopen_s()
原型:errno_t fopen_s( FILE** pFile, const char *filename, const char *mode ); 例子: char *filePath=&q ...