本文主要是介绍POJ - 3662(dijkstra+二分),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Telephone Lines
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
题意:有n个电线杆,有p条线路可选择。可以免费用k条,剩下的线路只需支付最长的那条线路。问最少费用为多少。
思路:直接求1-n的最短路不一定能连接所有的点。但由题目要求可以知道,我们只需要求出长度x满足条件(最短路径中大于x的线路等于k),x即为答案。所以可以二分答案,建一个邻接矩阵把Li>mid的线路标记为1,再求最短路,判断是否满足条件,接着二分,直到得到答案。
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <functional>
#define inf 0X3f3f3f3f
using namespace std;
typedef long long ll;
const int MAXN=1e4+10;
const int MAX=1000+10;int n,m,k;
int mapp[MAX][MAX],book[MAX],dis[MAX];
struct WAY{int u,v,w;
}way[10010];int cmp(const struct WAY a,const struct WAY b){return a.w<b.w;
}void get_map(int mid){for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)mapp[i][j]=inf;for(int i=0;i<m;i++){if(way[i].w<=mid) mapp[way[i].u][way[i].v]=mapp[way[i].v][way[i].u]=0;else mapp[way[i].u][way[i].v]=mapp[way[i].v][way[i].u]=1;}
}int dijkstra(){memset(book,0,sizeof(book));for(int i=1;i<=n;i++)dis[i]=mapp[1][i];book[1]=1;for(int k=1;k<=n;k++){int min=-1,t;for(int i=1;i<=n;i++)if(book[i]==0&&(min==-1||dis[i]<min)){min=dis[i];t=i;}book[t]=1;for(int i=1;i<=n;i++)if(book[i]==0&&dis[i]>dis[t]+mapp[t][i])dis[i]=dis[t]+mapp[t][i];}return dis[n];
}int main(){#ifdef ONLINE_JUDGE#elsefreopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);#endifscanf("%d%d%d",&n,&m,&k);for(int i=0;i<m;i++)scanf("%d%d%d",&way[i].u,&way[i].v,&way[i].w);sort(way,way+m,cmp);get_map(0);int flag=dijkstra();if(flag==inf||flag<=k){if(flag==inf) cout<<"-1"<<endl;else cout<<"0"<<endl;}else{int l=0,r=n,mid;while(l<=r){mid=(r+l)/2;get_map(way[mid].w);if(dijkstra()<=k) r=mid-1;else l=mid+1;}printf("%d\n",way[l].w);}return 0;
}
这篇关于POJ - 3662(dijkstra+二分)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!