题目大意:
多年以后,笨笨长大了,成为了电话线布置师。由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人。该市周围分布着N(1<=N<=1000)根据1……n顺序编号的废弃的电话线杆,任意两根线杆之间没有电话线连接,一共有p(1<=p<=10000)对电话杆可以拉电话线。其他的由于地震使得无法连接。
第i对电线杆的两个端点分别是ai,bi,它们的距离为li(1<=li<=1000000)。数据中每对(ai,bi)只出现一次。编号为1的电话杆已经接入了全国的电话网络,整个市的电话线全都连到了编号N的电话线杆上。也就是说,笨笨的任务仅仅是找一条将1号和N号电线杆连起来的路径,其余的电话杆并不一定要连入电话网络。
电信公司决定支援灾区免费为此市连接k对由笨笨指定的电话线杆,对于此外的那些电话线,需要为它们付费,总费用决定于其中最长的电话线的长度(每根电话线仅连接一对电话线杆)。如果需要连接的电话线杆不超过k对,那么支出为0.
请你计算一下,将电话线引导震中市最少需要在电话线上花多少钱?
思路:
我们可以二分一个值,高于他的就要用到电话线,那么跑一次最短路,如果最短路是大于K的那么就把二分的值调高,小于K就是合法的。
程序:
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<cstring>
#define N 1000000
using namespace std;
int last[N],cnt,ans,n,p,k,dis[N],x,y,z;
struct data{int w,to,next;}e[N];
queue <int> q;
void add(int x,int y,int w){
e[++cnt].to=y; e[cnt].w=w; e[cnt].next=last[x]; last[x]=cnt;
e[++cnt].to=x; e[cnt].w=w; e[cnt].next=last[y]; last[y]=cnt;
}
bool cheak(int x,int y){
if (x>y) return 1;
return 0;
}
bool spfa(int x){
while (!q.empty()) q.pop();
q.push(1);
memset(dis,0x3f,sizeof(dis));
dis[1]=0;
while (!q.empty()){
int u=q.front();
q.pop();
for (int i=last[u];i;i=e[i].next)
if (dis[u]+cheak(e[i].w,x)<dis[e[i].to]){
dis[e[i].to]=dis[u]+cheak(e[i].w,x);
q.push(e[i].to);
}
}
if (dis[n]<=k) return 1;
return 0;
}
int main(){
scanf("%d%d%d",&n,&p,&k);
for (int i=1;i<=p;i++){
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
}
int l=1,r=1000000;
ans=-1;
while (l!=r){
int mid=(l+r)>>1;
if (!spfa(mid)) l=mid+1;
else r=mid,ans=mid;
}
if (ans==-1) printf("-1");
else printf("%d",ans);
}