POJ_3662_Telephone_Lines_(二分+最短路)
描述
http://poj.org/problem?id=3662
给一张图,要将1与n连起来.可以有k条边免费,其他边自费,付费的值为所有自费边中最大的值.求最小付费.
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5932 | Accepted: 2152 |
Description
Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of the cables required to connect his farm to the phone system.
There are N (1 ≤ N ≤ 1,000) forlorn telephone poles conveniently numbered 1..N that are scattered around Farmer John's property; no cables connect any them. A total of P (1 ≤ P ≤ 10,000) pairs of poles can be connected by a cable; the rest are too far apart.
The i-th cable can connect the two distinct poles Ai and Bi, with length Li (1 ≤ Li ≤ 1,000,000) units if used. The input data set never names any {Ai, Bi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and N need to be connected by a path of cables; the rest of the poles might be used or might not be used.
As it turns out, the phone company is willing to provide Farmer John with K (0 ≤ K < N) lengths of cable for free. Beyond that he will have to pay a price equal to the length of the longest remaining cable he requires (each pair of poles is connected with a separate cable), or 0 if he does not need any additional cables.
Determine the minimum amount that Farmer John must pay.
Input
* Line 1: Three space-separated integers: N, P, and K
* Lines 2..P+1: Line i+1 contains the three space-separated integers: Ai, Bi, and Li
Output
*
Line 1: A single integer, the minimum amount Farmer John can pay. If it
is impossible to connect the farm to the phone company, print -1.
Sample Input
5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6
Sample Output
4
Source
分析
可以假定一个付费值m,所有<=m的都自己付费,>m的免费,然后使>m的边数a尽可能小,看是否可以使得a<=k.这里用二分即可.
统计a的最小值时,我们考虑:边分为免费边和自费边,要让免费边数量尽可能小,就把免费边赋值为1,自费边赋值为0,跑最短路,最后d[n]就是最少经过的免费边数量a.
另外,二分的标准如果是0~maxl的话就太大了,我们可以把每一条边的值存在f数组中,然后二分f[0]~f[p],这样会小很多.
注意:
1.如果自费最大费用(不用免费的了),还是不能成功,就输出-1.
Dijkstra:
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; const int maxn=,maxp=,INF=<<;
struct node
{
int to,w;
node(){}
node(int a,int b) : to(a),w(b){}
bool operator < (const node &a) const { return a.w>w; }
};
vector <node> g[maxn];
int n,p,k;
int d[maxn],f[maxp]; bool Dijkstra(int m)
{
for(int i=;i<=n;i++) d[i]=INF;
d[]=;
priority_queue <node> q;
q.push(node(,));
while(!q.empty())
{
int x=q.top().to;q.pop();
for(int i=;i<g[x].size();i++)
{
int y=g[x][i].to,dxy=g[x][i].w;
dxy=dxy>m ? : ;
if(d[y]>d[x]+dxy)
{
d[y]=d[x]+dxy;
q.push(node(y,d[y]));
}
}
}
return (d[n]<=k);
} void solve()
{
if(!Dijkstra(f[p])) { printf("-1\n"); return; }
int l=,r=p;
while(l<r)
{
int m=l+(r-l)/;
if(Dijkstra(f[m])) r=m;
else l=m+;
}
printf("%d\n",f[l]);
} void init()
{
scanf("%d%d%d",&n,&p,&k);
for(int i=;i<=p;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
g[a].push_back(node(b,c));
g[b].push_back(node(a,c));
f[i]=c;
}
sort(f+,f++p);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("phone.in","r",stdin);
freopen("phone.out","w",stdout);
#endif
init();
solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return ;
}
Spfa:
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; const int maxn=,maxp=,INF=<<;
struct node
{
int to,w;
node(){}
node(int a,int b) : to(a),w(b){}
};
vector <node> g[maxn];
int n,p,k;
int d[maxn],f[maxp];
bool vis[maxn]; bool Spfa(int m)
{
for(int i=;i<=n;i++) { d[i]=INF; vis[i]=false; }
d[]=; vis[]=true;
queue <int> q;
q.push();
while(!q.empty())
{
int x=q.front();
q.pop();
vis[x]=false;
for(int i=;i<g[x].size();i++)
{
int y=g[x][i].to,dxy=g[x][i].w;
dxy=dxy>m ? : ;
if(d[y]>d[x]+dxy)
{
d[y]=d[x]+dxy;
if(!vis[y])
{
vis[y]=true;
q.push(y);
}
}
}
}
return (d[n]<=k);
} void solve()
{
if(!Spfa(f[p])) { printf("-1\n"); return; }
int l=,r=p;
while(l<r)
{
int m=l+(r-l)/;
if(Spfa(f[m])) r=m;
else l=m+;
}
printf("%d\n",f[l]);
} void init()
{
scanf("%d%d%d",&n,&p,&k);
for(int i=;i<=p;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
g[a].push_back(node(b,c));
g[b].push_back(node(a,c));
f[i]=c;
}
sort(f+,f++p);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("phone.in","r",stdin);
freopen("phone.out","w",stdout);
#endif
init();
solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return ;
}
POJ_3662_Telephone_Lines_(二分+最短路)的更多相关文章
- BZOJ_1614_ [Usaco2007_Jan]_Telephone_Lines_架设电话线_(二分+最短路_Dijkstra/Spfa)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1614 分析 类似POJ_3662_Telephone_Lines_(二分+最短路) Dijks ...
- 二分+最短路 uvalive 3270 Simplified GSM Network(推荐)
// 二分+最短路 uvalive 3270 Simplified GSM Network(推荐) // 题意:已知B(1≤B≤50)个信号站和C(1≤C≤50)座城市的坐标,坐标的绝对值不大于100 ...
- P1462 通往奥格瑞玛的道路 (二分+最短路)
题目 P1462 通往奥格瑞玛的道路 给定\(n\)个点\(m\)条边,每个点上都有点权\(f[i]\),每条边上有边权,找一条道路,使边权和小于给定的数\(b\),并使最大点权最小. 解析 二分一下 ...
- 二分+最短路 UVALive - 4223
题目链接:https://vjudge.net/contest/244167#problem/E 这题做了好久都还是超时,看了博客才发现可以用二分+最短路(dijkstra和spfa都可以),也可以用 ...
- 2018.07.20 bzoj1614: Telephone Lines架设电话线(二分+最短路)
传送门 这题直接做显然gg" role="presentation" style="position: relative;">gggg,看这数据 ...
- 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)- D. Delivery Delays -二分+最短路+枚举
2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)- D. Delivery Delays -二分+最短路+枚举 ...
- Luogu P1951 收费站_NOI导刊2009提高(2) 二分 最短路
思路:二分+最短路 提交:1次 题解: 二分最后的答案. $ck()$: 对于每次的答案$md$跑$s,t$的最短路,但是不让$c[u]>md$的点去松弛别的边,即保证最短路不经过这个点.最后$ ...
- BZOJ 1614 [Usaco2007 Jan]Telephone Lines架设电话线 (二分+最短路)
题意: 给一个2e4带正边权的图,可以免费k个边,一条路径的花费为路径上边权最大值,问你1到n的最小花费 思路: 对于一个x,我们如果将大于等于x的边权全部免费,那么至少需要免费的边的数量就是 “设大 ...
- 二分+最短路判定 BZOJ 2709: [Violet 1]迷宫花园
BZOJ 2709: [Violet 1]迷宫花园 Sample Input 5 ######### # # # # # # # #S# # ##### # # ## # # # ### ### ## ...
随机推荐
- powerbulider9.0在数据窗口中实现滚动到新添加行
powerbuilder9.0对数据窗口进行增加行操作,然后实现滚动到指定行时,应先滚动到指定行dw_1.scrolltorow( row),然后设置新添加的行为当前行dw_1.setrow( row ...
- 是么是 API 和 SDK
API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问源码 ...
- IO流中的文件创建并且写入读取
package com.java.inoutputstreamDmeo.www; import java.io.File;import java.io.FileInputStream;import j ...
- LA 3177 Beijing Guards(二分法 贪心)
Beijing Guards Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the ...
- PAT_1002 写出这个数
宝宝不开心了.自从回家开始百练就上不去POJ也上不去,今天突然HDU也上不去了,PAT25分的题目都快更新完了.我就按顺序往下面更新了.回学校之后题目质量能高出不少= =. 问题描述: 读入一个自然数 ...
- ZeroBrane Lua脚本编辑器代码自动补全
简介 ZeroBrane Studio是一款支持代码提示.语法高亮.远程调试.代码分析.调试等功能的轻量级Lua IDE工具.可以去官网studio.zerobrane.com进行下载 ...
- 24种设计模式--原型模式【Prototype Pattern】
今天我们来讲原型模式,这个模式的简单程度是仅次于单例模式和迭代器模式,非常简单,但是要使用好这个模式还有很多注意事项.我们通过一个例子来解释一下什么是原型模式. 现在电子账单越来越流行了,比如你的信用 ...
- MySQL 查询某时间段范围内的数据 补零
1.创建基础表 CREATE TABLE num (i INT); INSERT INTO num (i) VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9) ...
- php 文件上传的基本方法
基本思路:1.HTML表单中 form中的enctype必为enctype="multipart/form-data",method = post 设置提交数据中的type = f ...
- session 保存在指定的数据表,cookie设置
首先建立数据表,可在ThinkPHP/Extend/Driver/Session/SessionDb.class.php中copy代码 在配置文件中配置: 'SESSION_TYPE' => ' ...