描述


http://poj.org/problem?id=3662

给一张图,要将1与n连起来.可以有k条边免费,其他边自费,付费的值为所有自费边中最大的值.求最小付费.

Telephone Lines
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_(二分+最短路)的更多相关文章

  1. BZOJ_1614_ [Usaco2007_Jan]_Telephone_Lines_架设电话线_(二分+最短路_Dijkstra/Spfa)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1614 分析 类似POJ_3662_Telephone_Lines_(二分+最短路) Dijks ...

  2. 二分+最短路 uvalive 3270 Simplified GSM Network(推荐)

    // 二分+最短路 uvalive 3270 Simplified GSM Network(推荐) // 题意:已知B(1≤B≤50)个信号站和C(1≤C≤50)座城市的坐标,坐标的绝对值不大于100 ...

  3. P1462 通往奥格瑞玛的道路 (二分+最短路)

    题目 P1462 通往奥格瑞玛的道路 给定\(n\)个点\(m\)条边,每个点上都有点权\(f[i]\),每条边上有边权,找一条道路,使边权和小于给定的数\(b\),并使最大点权最小. 解析 二分一下 ...

  4. 二分+最短路 UVALive - 4223

    题目链接:https://vjudge.net/contest/244167#problem/E 这题做了好久都还是超时,看了博客才发现可以用二分+最短路(dijkstra和spfa都可以),也可以用 ...

  5. 2018.07.20 bzoj1614: Telephone Lines架设电话线(二分+最短路)

    传送门 这题直接做显然gg" role="presentation" style="position: relative;">gggg,看这数据 ...

  6. 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 -二分+最短路+枚举 ...

  7. Luogu P1951 收费站_NOI导刊2009提高(2) 二分 最短路

    思路:二分+最短路 提交:1次 题解: 二分最后的答案. $ck()$: 对于每次的答案$md$跑$s,t$的最短路,但是不让$c[u]>md$的点去松弛别的边,即保证最短路不经过这个点.最后$ ...

  8. BZOJ 1614 [Usaco2007 Jan]Telephone Lines架设电话线 (二分+最短路)

    题意: 给一个2e4带正边权的图,可以免费k个边,一条路径的花费为路径上边权最大值,问你1到n的最小花费 思路: 对于一个x,我们如果将大于等于x的边权全部免费,那么至少需要免费的边的数量就是 “设大 ...

  9. 二分+最短路判定 BZOJ 2709: [Violet 1]迷宫花园

    BZOJ 2709: [Violet 1]迷宫花园 Sample Input 5 ######### # # # # # # # #S# # ##### # # ## # # # ### ### ## ...

随机推荐

  1. springmvc中url-pattern的大坑

    <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springfr ...

  2. Java_Web学习笔记_过滤器应用案例(解决全站字符乱码)

    解决全站字符乱码(POST和GET中文编码问题) servlet: l  POST:request.setCharacterEncoding(“utf-8”); l  GET: String user ...

  3. Ext.Net学习笔记04:Ext.Net布局

    ExtJS中的布局功能很强大,常用的布局有border.accordion.fit.hbox.vbox等,Ext.Net除了将这些布局进行封装以外,更是对border进行了一些非常实用的改进,让我们来 ...

  4. 11_关于SqlMapperConfig.xml

    [简述] SqlMapConfig.xml是Mybatis的全局配置文件,配置内容如下: 1.properties---------属性 2.settings-----------全局配置参数 3.t ...

  5. ceph入门学习链接

    https://tobegit3hub1.gitbooks.io/ceph_from_scratch/content/introduction/component.html

  6. 【转】Oracle中dual表的用途介绍

    原文:Oracle中dual表的用途介绍 [导读]dual是一个虚拟表,用来构成select的语法规则,oracle保证dual里面永远只有一条记录.我们可以用它来做很多事情. dual是一个虚拟表, ...

  7. SVG绘制矩形简单示例分享

    最近我初学HTML5,刚在一步步学习SVG,积累了一些个人心得和程序代码,希望和大家分享,今天分享“svg之矩形”部分 1.简单矩形 效果图如下: 关键代码: <svg xmlns=" ...

  8. 微信分享功能引入页面-控制分享时候调用的标题、图片、url和微信按钮隐藏显示控制

    1.设置分享调用的标题.图片.url预览. 2.控制右上角三个点按钮的隐藏显示(和底部工具栏的显示隐藏--未测试). 3.判断网页是否在微信中被调用. <!doctype html> &l ...

  9. 通过表名显示数据库中该表的表头和内容(mysql扩展库操作)

    编写一个函数,接收一个表名,然后把表的表头和内容显示在网页 <?php function readTab($tableName){ $conn=mysql_connect("local ...

  10. HHVM简介(译)

    原文链接:http://coderoncode.com/2013/07/24/introduction-hhvm.html “HHVM(HIpHop Virtual Machina)把PHP代码转换成 ...