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. logisticregression

    from numpy import * import random import time st = time.time() def loaddata(filename): fr = open(''. ...

  2. powerpoint无法输入中文怎么办|ppt文本框无法输入中文解决办法

    powerpoint文本框无法输入中文的情况不知大家是否遇到过呢?反正小编是遇到过这样的情况的,简直是急煞人也!那么powerpoint无法输入中文时应该怎么办呢?本节内容中小编就为大家带来ppt文本 ...

  3. Entity Framewor 学习笔记 (include + where)

    如果我们想在子查询做过滤的话应该怎样写呢? IEnumerable<Product> products = db.products.Include(p => p.colors.Whe ...

  4. Entity Framework with MySQL 学习笔记一(安装)

    声明 :  数据库是Mysql,本人的程度只到会写sql语句(不会储蓄过程), c# 会基本的ADO.NET数据库访问,LINQ基础. 这篇只做个人学习|温习作用. 新手可以参考,也请高手指正错误, ...

  5. Delphi判断进程是否存在(使用CreateToolhelp32Snapshot)

      program Project2; uses windows,TLHelp32; function FindProcess(AFileName:string):boolean; var hSnap ...

  6. Windows打印体系结构之Print Spooler概念与架构

    Windows打印体系结构之Print Spooler概念与架构Windows 思杰之路(陶菘) · 2016-09-06 22:07 房子好不好,对我而言始终都是肉体的栖居.对于灵魂,我从来不知道该 ...

  7. js中iframe的用法

    最近遇到的项目总是习惯左边一个树,点击每个树的节点右边出现相应的信息,遇到这种情况用iframe还是很简单的, 例如 : 页面文件 @section Tree{ <ul id="tre ...

  8. HDOJ 1248

    完全背包. 模版. 物品的价值等价于体积. #include <stdio.h> #include <string.h> using namespace std; int ma ...

  9. Java设计模式之简单工厂、工厂方法和抽象工厂

    在前面的学习中(参见前面的博客),我们学到了很多OO原则: 封装变化 多用组合,少用继承 针对接口/超类编程,不针对实现编程 松耦合 开闭原则 让我们从一个简单的类开始,看看如何将之改造成符合OO原则 ...

  10. 从一些简单代码实例彻底理解面向对象编程思想|OOP本质是什么?

    从Rob Pike 的 Google+上的一个推看到了一篇叫<Understanding Object Oriented Programming>的文章,我先把这篇文章简述一下,然后再说说 ...