POJ 3662 Telephone Lines(Dijkstra的邻接表存储)(可以作为模板题参考)
来源:互联网 发布:淘宝男士机械手表 编辑:程序博客网 时间:2024/06/07 06:49
Memory Limit: 65536KTotal Submissions: 7047
Accepted: 2583
Description
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 ofP (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 polesAi 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 andN 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 withK (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 11 2 53 1 42 4 83 2 35 2 93 4 74 5 6
Sample Output
4
思路:Dijkstra的邻接表
首先用bfs判断是否有着一条路径
接下来因为k条可以免去计算 所以可以用二分查找的方法
d数组为存储从1到该点大于查找值得路径的条数
#include <stdio.h>#include <iostream>#include <string.h>#include <queue>#include <algorithm>using namespace std;const int MAXV=1005;const int MAXE=50005;struct Node{int id,dis;//记录id 和记录有多少条路径比查找值大};bool operator<(const Node a,const Node b){return a.dis>b.dis;}struct edge{int v,next,cost;}e[MAXE];int head[MAXV],d[MAXV];int n,p,k;int cnt;bool bfs() //判断是否有从1到达n的路径{ int i,j,visit[1001]; memset(visit,0,sizeof(visit)); queue<int> Q; Q.push(1); visit[1]=1; while(!Q.empty()) { int u=Q.front(); Q.pop(); for(i=head[u];i!=-1;i=e[i].next) //邻接表的遍历 { int v=e[i].v; if(visit[v])continue; visit[v]=1; if(v==n)//当1有到达n的路径的时候return true; Q.push(v); } } return false;}int dijkstra(int ans){ priority_queue<Node> q;//优先的队列 距离大的在底下 距离小的在上面 Node temp,tt; bool visit[1001]; for(int i=1;i<=n;i++) {d[i]=1<<20;//d数组用来存储有多少条路径比查找值大visit[i]=0; } d[1]=0; temp.id=1; temp.dis=0; q.push(temp); while(!q.empty()) {tt=q.top();q.pop();int u=tt.id;if(visit[u])continue;visit[u]=1;for(int i=head[u];i!=-1;i=e[i].next){ int v=e[i].v; int t=e[i].cost>ans? 1:0;//如果路径比查找值大的话 赋值为1 否则 赋值为0 if(!visit[v]) { if(d[v]>d[u]+t) { d[v]=d[u]+t; tt.id=v; tt.dis=d[v]; q.push(tt); } }} } return d[n];//到n这个点 有多少点路径比查找值大的}int main(){ int from,to,cost; while(scanf("%d%d%d",&n,&p,&k)!=EOF) { cnt=1; memset(e,0,sizeof(e)); memset(head,-1,sizeof(head)); for(int i=0;i<p;i++)//邻接表存储 { scanf("%d%d%d",&from,&to,&cost); e[cnt].v=to;//因为是双向的 所以正反的邻接表都要设定 e[cnt].next=head[from]; e[cnt].cost=cost; head[from]=cnt++; e[cnt].v=from; e[cnt].next=head[to]; e[cnt].cost=cost; head[to]=cnt++; } if(!bfs()) { printf("-1\n"); continue; } int left=0,right=1000001,mid; while(left<=right)//二分查找的方法 { mid=(left+right)>>1; if(dijkstra(mid)<=k)//dijkstra函数返回的是总共有几条超过了所查询的路径路径长度right=mid-1; elseleft=mid+1; } printf("%d\n",right+1); }}
- POJ 3662 Telephone Lines(Dijkstra的邻接表存储)(可以作为模板题参考)
- POJ 3662 Telephone Lines (Dijkstra+二分)
- POJ Telephone Lines 二分+dijkstra
- POJ 3662 Telephone Lines
- POJ 3662: Telephone Lines
- POJ-3662-Telephone Lines
- POJ 3662 Telephone Lines
- POJ-3662Telephone Lines
- POJ 3662 Telephone Lines
- POJ 3662 Telephone Lines (SPFA、二分搜索)
- [POJ 3662] Telephone Lines (二分答案+SPFA)
- POJ 3662 Telephone Lines 电话线(二分答案+SPFA)
- Poj 3662 Telephone Lines(最短路+二分)
- poj 3662 Telephone Lines 图论
- poj 3662 Telephone Lines spfa算法的灵活运用
- 最短路模板(dijkstra+邻接表)
- 堆优化的dijkstra算法(以邻接表存储)
- POJ 3662 Telephone Lines 二分答案+djk
- [JZOJ5215]【HEOI、SXOI2017】组合数问题
- windows 概述
- UE4 UMG(unity的NGUI的关联理解)
- JS补充学习
- 合唱队
- POJ 3662 Telephone Lines(Dijkstra的邻接表存储)(可以作为模板题参考)
- c/c++中const的用法
- LeetCode 71. Simplify Path
- 一个整型数组里除了两个数字之外,其他的数字都出现了两次
- STL vector学习笔记
- Android Studio下 NDK配置与Jni开发流程
- C-C++语言结构体中定义另一个结构体指针的经验
- codeforces 559C Gerald and Giant Chess
- PHP 面向对象