POJ 3662 Telephone Lines(Dijkstra的邻接表存储)(可以作为模板题参考)

Telephone Lines
Time Limit: 1000MS
Memory Limit: 65536K
Total 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 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

思路: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;
			  else
				left=mid+1;
		 }
		 printf("%d\n",right+1);
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值