POJ3662 Telephone Lines( dijkstral + 二分 )
题目大意:要在顶点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 + 二分 )的更多相关文章
- poj3662 Telephone Lines【最短路】【二分】
http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ3662 Telephone Lines (dijkstra+二分)
Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncoop ...
- poj-3662 Telephone Lines 二分答案+最短路
链接:洛谷 POJ 题目描述 Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone co ...
- 【POJ3662】Telephone Lines dij + 二分答案
题目大意:给定一个 N 个顶点,M 条边的无向图,求一条从 1 号节点到 N 号节点之间的路径,使得第 K+1 大的边权最小,若 1 与 N 不连通,输出 -1. 最小化最大值一类的问题,采用二分答案 ...
- POJ-3662 Telephone Lines 二分+双端队列
题目传送门 题意:有n个点, p条路,每条道路有个花费Li, 然后现在要建一条1-n的路线,然后可以选k条道路免费, 然后可以在剩下的道路中选择价格最高的边支付费用, 求这个答案最小. 题解: 二分答 ...
- POJ3662 [USACO08JAN]Telephone Lines (二分答案/分层图求最短路)
这道题目有两种解法: 1.将每个点视为一个二元组(x,p),表示从起点到x有p条路径免费,相当于构建了一张分层图,N*k个节点,P*k条边.在这张图上用优先队列优化的SPFA算法求解,注意这里的d数组 ...
- POJ 3662 Telephone Lines (二分 + 最短路)
Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncoop ...
- POJ 3662 Telephone Lines(二分答案+SPFA)
[题目链接] http://poj.org/problem?id=3662 [题目大意] 给出点,给出两点之间连线的长度,有k次免费连线, 要求从起点连到终点,所用的费用为免费连线外的最长的长度. 求 ...
- POJ 3662 Telephone Lines【二分答案+最短路】||【双端队列BFS】
<题目链接> 题目大意: 在一个节点标号为1~n的无向图中,求出一条1~n的路径,使得路径上的第K+1条边的边权最小. 解题分析:直接考虑情况比较多,所以我们采用二分答案,先二分枚举第K+ ...
随机推荐
- ubuntu 下 github 使用方法 以及异常修改
接触github很长时间了,github有windows 跟 mac 版本,恶心的是现在在linux 下没有可视化界面的版本.所以对于很多没有怎么接触过github的人带来很大困难.话不多说,彪重点: ...
- 视图必须派生自 WebViewPage 或 WebViewPage错误解决方法
1,在每个视图上面添加 @inherits System.Web.Mvc.WebViewPage 2,将views中的web.config COPY到新的视图模版文件夹下,就可以了
- pin导致路由器死掉的解决方法
首先检测网卡: ifconfig -a 然后模拟端口: airmon-ng start wlan0 接下来用: airodump-ng mon0 扫描ap找到你pin死的路由器mac 用mdk3 做身 ...
- PHP 中的静态变量的简单使用
静态变量的初始化只能在第一次static 声明的时候进行,这些静态变量只能在声明他的函数中访问到. 例如: <?php function do_something(){ static $firs ...
- LeetCode _ Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- EF 5.0 帮助类 增删改查
原文地址:http://www.cnblogs.com/luomingui/p/3362813.html EF 5.0 帮助类 加入命名空间: using System; using System.D ...
- ZOJ2112--Dynamic Rankings (动态区间第k大)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- 使用python进行接口测试(二)
之前使用过urllib和urllib2做接口测试,在做的途中,感觉使用urllib2直接进行的get,post 请求并没有那么好用.作为测试人员,所需要的测试工具应当以方便为第一要务,测试的耗时只要是 ...
- 转载——Struts2中的constant详解
http://bhw1015.iteye.com/blog/1258441 通过对这些属性的配置,可以改变Struts 2 框架的一些默认行为,这些配置可以在struts.xml文件中完成,也可以在s ...
- Mongodb 条件查询
1.1 查询出所有数据的指定键(name ,age ,country) db.persons.find({},{name:1,age:1,country:1,_id:0}) 2.查询条件 2.查询条件 ...