训练指南 UVA- 11865(有向最小生成树 + 朱刘算法 + 二分)
layout: post
title: 训练指南 UVA- 11865(有向最小生成树 + 朱刘算法 + 二分)
author: "luowentaoaa"
catalog: true
mathjax: true
tags:
- 最小生成树
- 图论
- 训练指南
Stream My Contest
二分带宽,然后判断最小生成树是否小于cost值。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=100+50;
const ll inf=1000000000;;
/// 固定根的最小树型图,邻接矩阵写法
struct MDST{
int n;
int w[maxn][maxn]; ///边权
int vis[maxn]; ///访问标记,仅用来判断无解
int ans; ///计算答案
int removed[maxn]; ///每个点是否被删除
int cid[maxn]; ///所在圈编号
int pre[maxn]; ///最小入边的起点
int iw[maxn]; ///最小入边的权值
int max_cid; ///最大圈编号
void init(int n){
this->n=n;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)w[i][j]=inf;
}
void AddEdge(int u,int v,int cost){
w[u][v]=min(w[u][v],cost); ///重边取权值最小的
}
///从s出发能到达多少个结点
int dfs(int s){
int ans=1;
vis[s]=1;
for(int i=0;i<n;i++)if(!vis[i]&&w[s][i]<inf)ans+=dfs(i);
return ans;
}
///从u出发沿着pre指针找圈
bool cycle(int u){
max_cid++;
int v=u;
while(cid[v]!=max_cid){cid[v]=max_cid;v=pre[v];}
return v==u;
}
/// 计算u的最小入弧,入弧起点不得在圈c中
void update(int u){
iw[u]=inf;
for(int i=0;i<n;i++)
if(!removed[i]&&w[i][u]<iw[u]){
iw[u]=w[i][u];
pre[u]=i;
}
}
///根节点为s,如果失败返回false
bool solve(int s){
memset(vis,0,sizeof(vis));
if(dfs(s)!=n)return false;
memset(removed,0,sizeof(removed));
memset(cid,0,sizeof(cid));
for(int u=0;u<n;u++)update(u);
pre[s]=s;iw[s]=0; /// 根结点特殊处理
ans=max_cid=0;
for(;;){
bool have_cycle=false;
for(int u=0;u<n;u++)if(u!=s&&!removed[u]&&cycle(u)){
have_cycle=true;
/// 以下代码缩圈,圈上除了u之外的结点均删除
int v=u;
do{
if(v!=u)removed[v]=1;
ans+=iw[v];
/// 对于圈外点i,把边i->v改成i->u(并调整权值);v->i改为u->i
/// 注意圈上可能还有一个v'使得i->v'或者v'->i存在,因此只保留权值最小的i->u和u->i
for(int i=0;i<n;i++)if(cid[i]!=cid[u]&&!removed[i]){
if(w[i][v]<inf)w[i][u]=min(w[i][u],w[i][v]-iw[v]);
w[u][i]=min(w[u][i],w[v][i]);
if(pre[i]==v)pre[i]=u;
}
v=pre[v];
}while(v!=u);
update(u);
break;
}
if(!have_cycle)break;
}
for(int i=0;i<n;i++)
if(!removed[i])ans+=iw[i];
return true;
}
};
MDST solver;
struct Edge{
int u,v,b,c;
bool operator <(const Edge& rhs)const{
return b>rhs.b;
}
};
const int maxm=10000+10;
int n,m,C;
Edge edges[maxm];
bool check(int cnt){
solver.init(n);
for(int i=0;i<cnt;i++)
solver.AddEdge(edges[i].u,edges[i].v,edges[i].c);
if(!solver.solve(0))return false;
return solver.ans<=C;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int t;
/* int a=inf;
cout<<a<<endl<<1000000000<<endl;*/
cin>>t;
while(t--){
cin>>n>>m>>C;
for(int i=0;i<m;i++)cin>>edges[i].u>>edges[i].v>>edges[i].b>>edges[i].c;
sort(edges,edges+m);
int l=1,r=m,ans=-1;
while(l<=r){
int mid=(l+r)/2;
if(check(mid)){ans=edges[mid-1].b;r=mid-1;}
else l=mid+1;
}
if(ans<0)cout<<"streaming not possible."<<endl;
else cout<<ans<<" kbps"<<endl;
}
return 0;
}
训练指南 UVA- 11865(有向最小生成树 + 朱刘算法 + 二分)的更多相关文章
- uva11865 朱刘算法+二分
这题说的需要最多花费cost元来搭建一个比赛网络,网络中有n台机器,编号为0 - n-1其中机器0 为服务器,给了n条线有向的和他们的花费以及带宽 计算,使得n台连接在一起,最大化网络中的最小带宽, ...
- UVA-11865 Stream My Contest (朱-刘 算法+二分)
题目大意:有一张n个顶点,m条边的有向图,根节点为0.每条边有两个权值,一个是费用c,一个是长度b.问在总费用不超过cost的情况下选出若干条边,使得n个点连通时的边的最短长度的最大值是多少. 题目分 ...
- Uva11183-Teen Girl Squad(有向图最小生成树朱刘算法)
解析: 裸的有向图最小生成树 代码 #include<cstdio> #include<cstring> #include<string> #include< ...
- 训练指南 UVA - 11354(最小生成树 + 倍增LCA)
layout: post title: 训练指南 UVA - 11354(最小生成树 + 倍增LCA) author: "luowentaoaa" catalog: true ma ...
- 训练指南 UVA - 11419(二分图最小覆盖数)
layout: post title: 训练指南 UVA - 11419(二分图最小覆盖数) author: "luowentaoaa" catalog: true mathjax ...
- 训练指南 UVA - 11383(KM算法的应用 lx+ly >=w(x,y))
layout: post title: 训练指南 UVA - 11383(KM算法的应用 lx+ly >=w(x,y)) author: "luowentaoaa" cata ...
- 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束)
layout: post title: 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束) author: "luowentaoaa" catal ...
- 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环)
layout: post title: 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环) author: "luowentaoaa" catalog: ...
- 训练指南 UVA - 10917(最短路Dijkstra + 基础DP)
layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: tr ...
随机推荐
- ARC075 E.Meaningful Mean(树状数组)
题目大意:给定n和k,问an中有多少子区间的平均值大于等于k 很巧妙的一个式子,就是如果一个区间[l, r]满足条件 那么则有 sum[r] - sum[l-1] >= (r-l+1)*k 整理 ...
- 洛谷 P2387 [NOI2014]魔法森林 解题报告
P2387 [NOI2014]魔法森林 题目描述 为了得到书法大家的真传,小 E 同学下定决心去拜访住在魔法森林中的隐 士.魔法森林可以被看成一个包含 n 个节点 m 条边的无向图,节点标号为 1,2 ...
- Topcoder SRM579 1000pts
石头剪刀布QAQ 一看是个很油的概率dp 首先一看你很快能得出状态的表示F[i][r][p][s] 然后只要考虑r,p,s出现的次数来进行概率dp就好了 具体实现的时候细节很多(少) 如果预处理一下组 ...
- [COGS 2089.] 平凡的测试数据 带权并查集
差点就撸上LCT了....... 带权并查集就是在并查集的基础上稍作修改,我的用穿址实现的有人用记录原父亲来实现. #include<cstdio> #define N 300010 us ...
- Palindrome [Manecher]
Palindrome Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 12214 Accepted: 4583 Descript ...
- java实现极简的LRU算法
import java.util.LinkedHashMap;import java.util.Map; /** * LRU (Least Recently Used) */public class ...
- sysctl -P net.bridge.bridge-nf-call-ip6tables报错解决办法
问题症状 修改 linux 内核文件 #vi /etc/sysctl.conf后执行sysctl -P 报错 error: "net.bridge.bridge-nf-call-ip6ta ...
- ansible+docker
1.准备镜像: 1007 docker run -itd --name client2 ff37bc5ab732 1008 docker run -itd --name client ff37bc5a ...
- 清理/var/spool/clientmqueue目录释放大量空间
清理/var/spool/clientmqueue目录可以释放大量空间,具体命令是:ls | xargs rm -f 文件太大,rm -rf会由于参数太多而无法删除,所以需要用上面的命令. “Argu ...
- POJ 2456 Aggressive cows---二分搜索法
///3.最大化最小值 /** POJ 2456 Aggressive cows Q:一排牛舍有N (2 <= N <= 100,000) 个,位置为x1,...,xN (0 <= ...