POJ3662,洛谷1948 [USACO08JAN]电话线Telephone Lines

一叶落寞,万物失色。

链接:洛谷的:https://www.luogu.org/problem/show?pid=1948

           POJ的:http://poj.org/problem?id=3662

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.

多年以后,笨笨长大了,成为了电话线布置师。由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人。该市周围分布着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.

请你计算一下,将电话线引导震中市最少需要在电话线上花多少钱?

输入输出格式

输入格式:

输入文件的第一行包含三个数字n,p,k;

第二行到第p+1行,每行分别都为三个整数ai,bi,li。

输出格式:

一个整数,表示该项工程的最小支出,如果不可能完成则输出-1.

输入输出样例

输入样例#1:
5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6
输出样例#1:
4

题目的思路比较的巧妙,离散后,二分一下边的长度,然后在边上开一个flag,用来作最短路,也即是用二分的那个长度来与原来的边长来比较,长啦flag就是1短为0,用flag作下最短路,再用来与k相比,不断二分,下面是代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值