[bzoj1614][Usaco2007Jan]Telephone Lines 架设电话线_二分答案_最短路
Telephone Lines bzoj-1614 Usaco-2007Jan
题目大意:给你一个n个点m条边的带边权无向图,求最短路。可以选取k条边免费。
注释:$1\le n\le 10^3$,$1\le m\le 10^5$
想法:一眼分层图最短路啊!
我都想出来了就上网查一下题解吧
卧槽??二分+spfa??
其实这个题不用分层图
我们二分答案,二分出最大值,然后将图中所有比mid大的边设为1,小的设为0,然后跑最短路。如果最短路的值比k大,说明最大值一定比二分的mid大。因为我的最短路值就代表着我需要将多少条边免费。
最后,附上丑陋的代码... ...
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#define N 1000+50
#define M 20000+50
using namespace std;
int head[N],dis[N];
const int inf = 0x7fffff;
int pre[N],from[N];
struct graph
{
int next,to,val;
int deval;
graph () {}
graph (int _next,int _to,int _val)
:next(_next),to(_to),val(_val){}
}edge[M];
int cnt = 0;
inline void add(int x,int y,int z)
{
edge[++cnt] = graph(head[x],y,z);
head[x]=cnt;
edge[++cnt] = graph(head[y],x,z);
head[y]=cnt;
}
int S,T;
bool in[N];
queue<int>Q;
void spfa()
{
memset(dis,0x3f,sizeof dis);
memset(in,false,sizeof in);
memset(pre,0,sizeof pre);
memset(from,0,sizeof from);
Q.push(S),dis[S]=0;in[S]=1;
while(!Q.empty())
{
int tt=Q.front();Q.pop();in[tt]=false;
for(int i=head[tt];i;i=edge[i].next)
if(dis[edge[i].to]>dis[tt]+edge[i].deval)
{
dis[edge[i].to]=dis[tt]+edge[i].deval;
pre[edge[i].to] = tt;from[edge[i].to] = i;
if(!in[edge[i].to])
{
Q.push(edge[i].to);
in[edge[i].to]=1;
}
}
}
}
int n,m,k;
bool check(int mid)
{
for(int i=1;i<=cnt;++i)
{
if(edge[i].val<=mid)edge[i].deval = 0;
else edge[i].deval = 1;
}
spfa();
if(dis[T]>k)
return false;
return true;
}
inline int read()
{
int x=0,f=1;char ch = getchar ();
while (ch < '0' || ch > '9') {if (ch == '-')f=-1;ch = getchar();}
while (ch >='0' && ch <='9') {x=(x<<1)+(x<<3)+ch -'0';ch = getchar();}
return x*f;
}
int main()
{
n=read(),m=read(),k=read();
S = 1,T = n;
int l = 0 , r = 0;
for(int i=1;i<=m;++i)
{
int x=read(),y=read(),z=read();
add(x,y,z); l = min(l,z),r = max(r,z);
}
int ans = inf;
while(l<r)
{
int mid = (l+r)>>1;
if(check(mid))
{
int now = n;
int tmp = 0;
while(now ^ 1)
{
if(edge[from[now]].deval == 0)
tmp = max(tmp,edge[from[now]].val);
now = pre[now];
}
ans = min(ans,tmp);
r = mid;
}
else
l = mid + 1;
}
printf("%d\n",ans == inf ? -1 : ans);
}
小结:图论真神奇。
[bzoj1614][Usaco2007Jan]Telephone Lines 架设电话线_二分答案_最短路的更多相关文章
- 2018.07.20 bzoj1614: Telephone Lines架设电话线(二分+最短路)
传送门 这题直接做显然gg" role="presentation" style="position: relative;">gggg,看这数据 ...
- bzoj 1614: [Usaco2007 Jan]Telephone Lines架设电话线【二分+spfa】
二分答案,然后把边权大于二分值的的边赋值为1,其他边赋值为0,然后跑spfa最短路看是否满足小于等于k条边在最短路上 #include<iostream> #include<cstd ...
- BZOJ1614:[USACO]Telephone Lines架设电话线(二分,最短路)
Description FarmerJohn打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司 支付一定的费用.FJ的农场周围分布着N(1<=N< ...
- BZOJ1614: [Usaco2007 Jan]Telephone Lines架设电话线
1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 892 Solved: ...
- [Usaco2007 Jan]Telephone Lines架设电话线(最短路,二分)
[Usaco2007 Jan]Telephone Lines架设电话线 Description FarmerJohn打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向 ...
- BZOJ 1614: [Usaco2007 Jan]Telephone Lines架设电话线
题目 1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec Memory Limit: 64 MB Description Farm ...
- 【bzoj1614】[Usaco2007 Jan]Telephone Lines架设电话线 二分+SPFA
题目描述 Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N <= 1 ...
- bzoj 1614 Telephone Lines架设电话线 - 二分答案 - 最短路
Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N ...
- 【SPFA+二分答案】BZOJ1614- [Usaco2007 Jan]Telephone Lines架设电话线
沉迷于刷水 以前的那个二分写法过不了QAQ 换了一种好像大家都比较常用的二分.原因还不是很清楚. [题目大意] 给出一张图,可以将其中k条边的边权减为0,求1到n的路径中最长边的最小值. [思路] 二 ...
随机推荐
- 解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合”
解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合” 最近更新: 2013-2-15 587 很少写WinForm程序第一次使用ListBox控件就遇到了比 ...
- bzoj 2252 [ 2010 Beijing wc ] 矩阵距离 —— 多源bfs
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2252 又没能自己想出来... 一直在想如何从每个1开始广搜更新答案,再剪剪枝,什么遇到1就不 ...
- 网上订餐系统的SQL SERVER 2005数据库连接
- [Apple开发者帐户帮助]二、管理你的团队(1)邀请团队成员
组织可以选择向其开发团队添加其他人员.如果您已加入Apple开发者计划,您将在App Store Connect中管理团队成员.有关详细信息,请转到在App Store Connect帮助中添加用户. ...
- 机器学习——Day 2 简单线性回归
写在开头 由于某些原因开始了机器学习,为了更好的理解和深入的思考(记录)所以开始写博客. 学习教程来源于github的Avik-Jain的100-Days-Of-MLCode 英文版:https:// ...
- python 10:len(list)(获取列表长度)以及负访问性
bicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(len(bicycles)) #获取某列表长度,即列表元素个数 pr ...
- python请求服务器时如何隐藏User-Agent
本文结合上一篇文章“python利用有道翻译实现“语言翻译器”的功能”的实现代码,对其进行加工,实现请求服务器时隐藏User-Agent. python实现隐藏User-Agent的一般做法有两种: ...
- POJ 1330 Tarjan LCA、ST表(其实可以数组模拟)
题意:给你一棵树,求两个点的最近公共祖先. 思路:因为只有一组询问,直接数组模拟好了. (写得比较乱) 原题请戳这里 #include <cstdio> #include <bits ...
- SAP computer之input and MAR
Input and MAR Below the program counter is the input and MAR block. It includes the address and data ...
- 调试程序时找不到DLL的解决办法
最近调试程序的经常弹出找不到DLL.只好一个个把DLL拷贝到程序目录下(我是拷贝到源文件目录,也有人说是Debug目录). 其实可以这么设置: 项目属性->配置属性->调试->工作目 ...