POJ3662 Telephone Lines

    题目大意:要在顶点1到顶点n之间建一条路径,假设这条路径有m条边,其中有k条边是免费的,剩余m-k条边是要收费的,

        求这m-k条边中花费最大的一条边的最小花费.

    让m条边中原本花费最大的k条边成为免费的边,则这时m-k条边中花费最大的一条边的花费最小.

    二分枚举m-k条边中花费最大的一条边的最小花费x,dijkstra求最短路径时,将花费大于x的边的花费设为1(花费为INF的边不变),花费小于等于x的边设为

0,则d[v-1]中返回的就是花费大于x的边数,当返回值小余等于k时,说明mid小了,ub=mid,否则,lb=mid+1;

  最后输出mid或lb即可

    一开始我的dijkstra未用队列优化,954ms飘过,用邻接矩阵存储时一开始一定要把所有边都初始化为INF,对cost[v][u]判断时,花费为INF的边不变

    未优化的dijkstra

    

/*
* Created: 2016年04月03日 14时11分34秒 星期日
* Author: Akrusher
*
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define in(n) scanf("%d",&(n))
#define in2(x1,x2) scanf("%d%d",&(x1),&(x2))
#define in3(x1,x2,x3) scanf("%d%d%d",&(x1),&(x2),&(x3))
#define inll(n) scanf("%I64d",&(n))
#define inll2(x1,x2) scanf("%I64d%I64d",&(x1),&(x2))
#define inlld(n) scanf("%lld",&(n))
#define inlld2(x1,x2) scanf("%lld%lld",&(x1),&(x2))
#define inf(n) scanf("%f",&(n))
#define inf2(x1,x2) scanf("%f%f",&(x1),&(x2))
#define inlf(n) scanf("%lf",&(n))
#define inlf2(x1,x2) scanf("%lf%lf",&(x1),&(x2))
#define inc(str) scanf("%c",&(str))
#define ins(str) scanf("%s",(str))
#define out(x) printf("%d\n",(x))
#define out2(x1,x2) printf("%d %d\n",(x1),(x2))
#define outf(x) printf("%f\n",(x))
#define outlf(x) printf("%lf\n",(x))
#define outlf2(x1,x2) printf("%lf %lf\n",(x1),(x2));
#define outll(x) printf("%I64d\n",(x))
#define outlld(x) printf("%lld\n",(x))
#define outc(str) printf("%c\n",(str))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mem(X,Y) memset(X,Y,sizeof(X));
typedef vector<int> vec;
typedef long long ll;
typedef pair<int,int> P;
const int dx[]={,,-,},dy[]={,,,-};
const int INF=0x3f3f3f3f;
const ll mod=1e9+;
ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
const bool AC=true; int cost[][];
int d[];
bool used[];
int V;
const int MAX_L=;
int dijkstra(int s,int x){
int pay;
fill(d,d+V,INF);
fill(used,used+V,false);
d[s]=;
while(true){
int v=-;
for(int u=;u<V;u++){ //从未使用的顶点中选取一个距离最小的顶点
if(!used[u]&&(v==-||d[v]>d[u])) v=u; //注意此处是v=u;(wa了半天)
}
if(v==-) break; //所有的顶点都被使用过了;
used[v]=true; rep(u,,V){ //更新最短距离
if(cost[v][u]>x&&cost[v][u]!=INF) pay=;//注意此处的判断,大于x的还有可能为INF,此处是pay=1,不是cost[v][u]=1;
else if(cost[v][u]<=x) pay=; //等于x的电缆线不需要花钱,也为所求的最大花费的电缆线
else if(cost[v][u]==INF) pay=INF; //一开始忘了判断这个,wa的不要不要的
d[u]=min(d[u],d[v]+pay);
}
}
return d[V-];//返回的是比x大的个数
}
int main()
{
int n,p,k,a,b,c,lb,ub,mid;
in3(n,p,k);
V=n;
fill(cost[],cost[]+*,INF);
rep(i,,p){
in3(a,b,c);
a--;b--;
cost[a][b]=c;
cost[b][a]=c;
}
lb=,ub=MAX_L+;//边的最大值为1000000
while(ub>lb){ //二分时mid总是向下取整,区间小的时候让lb=mid+1,相等时让ub=mid则不会陷入死循环
mid=(ub+lb)>>;//位移运算符更高效
if(dijkstra(,mid)<=k) ub=mid;
else lb=mid+;
}
if(lb==MAX_L+) printf("-1\n");
else out(ub);
return ;
}

一下是用优先队列优化,时间是125ms;

  

/*
* Created: 2016年04月03日 14时11分34秒 星期日
* Author: Akrusher
*
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define in(n) scanf("%d",&(n))
#define in2(x1,x2) scanf("%d%d",&(x1),&(x2))
#define in3(x1,x2,x3) scanf("%d%d%d",&(x1),&(x2),&(x3))
#define inll(n) scanf("%I64d",&(n))
#define inll2(x1,x2) scanf("%I64d%I64d",&(x1),&(x2))
#define inlld(n) scanf("%lld",&(n))
#define inlld2(x1,x2) scanf("%lld%lld",&(x1),&(x2))
#define inf(n) scanf("%f",&(n))
#define inf2(x1,x2) scanf("%f%f",&(x1),&(x2))
#define inlf(n) scanf("%lf",&(n))
#define inlf2(x1,x2) scanf("%lf%lf",&(x1),&(x2))
#define inc(str) scanf("%c",&(str))
#define ins(str) scanf("%s",(str))
#define out(x) printf("%d\n",(x))
#define out2(x1,x2) printf("%d %d\n",(x1),(x2))
#define outf(x) printf("%f\n",(x))
#define outlf(x) printf("%lf\n",(x))
#define outlf2(x1,x2) printf("%lf %lf\n",(x1),(x2));
#define outll(x) printf("%I64d\n",(x))
#define outlld(x) printf("%lld\n",(x))
#define outc(str) printf("%c\n",(str))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mem(X,Y) memset(X,Y,sizeof(X));
typedef vector<int> vec;
typedef long long ll;
typedef pair<int,int> P;
const int dx[]={,,-,},dy[]={,,,-};
const int INF=0x3f3f3f3f;
const ll mod=1e9+;
ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
const bool AC=true; struct edge{int to,cost;};
typedef pair<int,int> P; //first是最短距离,second是顶点编号
int V;
vector<edge> G[];
int d[];
const int MAX_L=;
int dijkstra(int s,int x){
priority_queue <P,vector<P>,greater<P> > que;
fill(d,d+V,INF);
d[s]=;
que.push(P(,s));
while(!que.empty()){
P p=que.top();que.pop();
int v=p.second;
if(d[v]<p.first) continue;
rep(i,,G[v].size()){
edge e=G[v][i];
if(e.cost>x) e.cost=;
else e.cost=;
if(d[e.to]>d[v]+e.cost){
d[e.to]=d[v]+e.cost;
que.push(P(d[e.to],e.to));
}
}
}
return d[V-];//返回的是比x大的个数
}
int main()
{
int n,p,k,a,b,c,lb,ub,mid;
in3(n,p,k);
V=n; rep(i,,p){
in3(a,b,c);
a--;b--;
edge e;
e.to=b;
e.cost=c;
G[a].push_back(e);
e.to=a;
e.cost=c;
G[b].push_back(e);//没有重边才能这样赋值
}
lb=,ub=MAX_L+;//边的最大值为1000000
while(ub>lb){ //二分时mid总是向下取整,区间小的时候让lb=mid+1,相等时让ub=mid则不会陷入死循环
mid=(ub+lb)>>;//位移运算符更高效
if(dijkstra(,mid)<=k) ub=mid;
else lb=mid+;
}
if(lb==MAX_L+) printf("-1\n");
else out(ub);
return ;
}

    

POJ3662 Telephone Lines( dijkstral + 二分 )的更多相关文章

  1. poj3662 Telephone Lines【最短路】【二分】

    http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  2. POJ3662 Telephone Lines (dijkstra+二分)

    Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncoop ...

  3. poj-3662 Telephone Lines 二分答案+最短路

    链接:洛谷 POJ 题目描述 Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone co ...

  4. 【POJ3662】Telephone Lines dij + 二分答案

    题目大意:给定一个 N 个顶点,M 条边的无向图,求一条从 1 号节点到 N 号节点之间的路径,使得第 K+1 大的边权最小,若 1 与 N 不连通,输出 -1. 最小化最大值一类的问题,采用二分答案 ...

  5. POJ-3662 Telephone Lines 二分+双端队列

    题目传送门 题意:有n个点, p条路,每条道路有个花费Li, 然后现在要建一条1-n的路线,然后可以选k条道路免费, 然后可以在剩下的道路中选择价格最高的边支付费用, 求这个答案最小. 题解: 二分答 ...

  6. POJ3662 [USACO08JAN]Telephone Lines (二分答案/分层图求最短路)

    这道题目有两种解法: 1.将每个点视为一个二元组(x,p),表示从起点到x有p条路径免费,相当于构建了一张分层图,N*k个节点,P*k条边.在这张图上用优先队列优化的SPFA算法求解,注意这里的d数组 ...

  7. POJ 3662 Telephone Lines (二分 + 最短路)

    Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncoop ...

  8. POJ 3662 Telephone Lines(二分答案+SPFA)

    [题目链接] http://poj.org/problem?id=3662 [题目大意] 给出点,给出两点之间连线的长度,有k次免费连线, 要求从起点连到终点,所用的费用为免费连线外的最长的长度. 求 ...

  9. POJ 3662 Telephone Lines【二分答案+最短路】||【双端队列BFS】

    <题目链接> 题目大意: 在一个节点标号为1~n的无向图中,求出一条1~n的路径,使得路径上的第K+1条边的边权最小. 解题分析:直接考虑情况比较多,所以我们采用二分答案,先二分枚举第K+ ...

随机推荐

  1. PHPCMS V9二次开发]自定义字段模型-文本组

    phpcms v9,我们在做类似于酒店房型等类型的时候,需要用到文本组字段模型,但phpcms并未提供该模型.如下图所示效果: 展示效果如下: 步骤/方法 打开phpcms\modules\conte ...

  2. Ajax 技术原理(转)

    Ajax 技术原理 2010-01-04 原文出处:http://www.nowamagic.net/ajax/ajax_AjaxPrinciple.php 在写这篇文章之前,曾经写过一篇关于AJAX ...

  3. 利用google浏览器开发者工具调试网页(详)

    前端程序员或者在校大学生正在开发网页,如果想要测试或者通过测试优化网页结构,该怎么办呢?这就需要用到一款工具,chrome浏览器的开发者工具?本文写给尚不熟悉这个开发者工具的同学们或者同行们,话不多说 ...

  4. sql对日期的处理,一个存储过程示例

    IF v_docType = 3 THEN update T_PATIENT_INFO set USER_NAME =userName ,SEX = v_sex,BIRTHDAY = to_date( ...

  5. Android 常用对话框Dialog封装

    Android 6种 常用对话框Dialog封装 包括: 消息对话框.警示(含确认.取消)对话框.单选对话框. 复选对话框.列表对话框.自定义视图(含确认.取消)对话框 分别如下图所示:       ...

  6. Intent携带额外的数据的方法

    1.putExtras(Bundle data):向Intent中放入需要“携带”的数据.2.putXxx(String key,Xxx data):向Bundle放入Int.Long等各种类型的数据 ...

  7. 基本排序算法:Python实现

    基本排序算法,包括冒泡排序,插入排序,选择排序,堆排序,快速排序等. [冒泡排序] 复杂度是n*n #coding:utf8 #author:HaxtraZ #description:冒泡排序 def ...

  8. vbox端口转发

    端口转发:setting->network->adapter:attached to NAT.port forwarding rules->name    protocol     ...

  9. [LeetCode] 55. Jump Game 解题思路

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  10. APP纯黑盒测试---某些可以试试的操作

    一.多次快速点击一处功能入口: 该测试方法可以在某些应用中打开俩次目标界面,举一些具体一点的例子: 1.比如现在很多APP需要登陆,如果打开了俩次登录页面,就容易造成登录成功后应用跳转界面又是登录界面 ...