poj 3662 Telephone Linse 二分答案 +dijkstra

F - Telephone Lins e
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  POJ 3662

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 {AiBi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and 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: NP, and K
* Lines 2..P+1: Line i+1 contains the three space-separated integers: AiBi, 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

#include 
#include 
#include 
#include 
#include 
#include 

#define MAXN 20001
#define MAX 1001
#define INF 999999999

using namespace std;

int n, p, k;

struct Edge
{
	int v, w, next;
	int flag;
}edge[MAXN];

int head[MAX];
int weight[MAXN];
int dis[MAXN];
bool vis[MAXN];
int e;

void add(int u, int v, int w)
{
	edge[e].v = v;
	edge[e].w = w;
	edge[e].next = head[u];
	head[u] = e++;
}

void init()
{
	e = 0;
	memset(head, -1, sizeof(head));
}

int dijkstra(int s, int e)
{
	for (int i = 1; i <= n; i++)
	{
		dis[i] = INF;
		vis[i] = false;
	}
	
	for (int i = head[1]; i != -1; i = edge[i].next)
	{
		int v = edge[i].v;
		dis[v] = edge[i].flag;
	}
	
	
	dis[s] = 0;
	vis[s] = true;
	
	for (int i = 2; i <= n; i++)
	{
		int mi = INF;
		int u = 1;
		
		for (int j = 1; j <= n; j++)
		{
			if (!vis[j] && dis[j] < mi)
			{
				mi = dis[j];
				u = j;
			}
		}
		
		vis[u] = true;
		
		for (int j = head[u]; j != -1; j = edge[j].next)
		{
			int v = edge[j].v;
			
			if (!vis[v] && dis[u] + edge[j].flag < dis[v])
			{
				dis[v] = dis[u] + edge[j].flag;
			}
		}
	}
	
	return dis[n];
}

void solve()
{
	weight[p + 1] = 0;
	
	sort(weight, weight + p + 1);
	
	int low = 0, high = p;
	int mid = high;
	int flag = false;
	
	while (low <= high)
	{
		for (int i = 1; i <= n; i++)
		{
			for (int j = head[i]; j != -1; j = edge[j].next)
			{
				if (edge[j].w > weight[mid])
				{
					edge[j].flag = 1;
				}
				else
				{
					edge[j].flag = 0;
				}
			}
		}
		
		int dist = dijkstra(1, n);
			
		if (dist <= k)
		{
			high = mid - 1;
		}
		else
		{
			low = mid + 1;
		}
		
		if (dist != INF)
		{
			flag = true;
		}
		mid = (low + high) >> 1;
	}
	
	if (!flag)
	{
		printf("-1\n");
	}
	else
	{
		printf("%d\n", weight[low]);
	}
}

void input()
{
	int a, b, l;
	
	init();
	
 	scanf("%d %d %d", &n, &p, &k);
	
	for (int i = 0; i < p; i++)
	{
		scanf("%d %d %d", &a, &b, &l);
		weight[i] = l;
		add(a, b, l);
		add(b, a, l);
	}
	
	solve();
}

int main()
{
	input();
	return 0;
}


你可能感兴趣的:(poj 3662 Telephone Linse 二分答案 +dijkstra)