Logu P1948 [Usaco08jan] telephone line telephone lines__ two points

Source: Internet
Author: User
Topic Description

Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company are uncooperative, so him 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 pro Perty; No cables connect any them. A total of P (1≤p≤10,000) pairs of poles can is 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 are at the farm. Poles 1 and N need to is connected by a path of cables; The rest of the poles might be used or might to be used.

As it turns out, the phone company is willing to provide farmer John with K (0≤k < N) lengths of cable of. Beyond that he'll 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 it does not need any additional cables.

Determine the minimum amount that farmer John must pay.

Years later, stupid grew up and became a telephone line planner. Because the earthquake caused the telephone line of a city to be completely damaged, stupid is responsible for receiving the epicenter of the city. The city is distributed around N (1<=n<=1000) According to the 1......N sequential number of discarded telephone poles, no two wire rods between the telephone line connection, there is a total of P (1<=p<=10000) to the telephone pole can pull the phone line. Others are unable to connect due to earthquakes.

The first two endpoints of the Poles are ai,bi, and their distances are Li (1<=li<=1000000). Each pair (AI,BI) in the data appears only once. The telephone bar, numbered 1, has been connected to the national telephone network, and the entire city's telephone line is connected to the numbered N telephone poles. That is to say, the stupid task is simply to find a way to connect the number 1th and N telephone poles, the rest of the phone is not necessarily connected to the telephone network.

The telecom company decided to support the disaster area free for this city to connect K to the telephone poles specified by the stupid, for those telephone lines that are in addition to pay for them, the total cost depends on the length of the longest telephone line (each telephone line is connected to only a pair of telephone poles). If you need to connect to a phone bar that does not exceed the K pair, the expense is 0.

Please calculate how much you need to spend on the telephone line to direct your telephone line to the epicenter of the city. input and output formats Input Format:

The first line of the input file contains three digital n,p,k;

The second line to line p+1, each row is three integer ai,bi,li.

output Format:

An integer that represents the minimum cost of the project and outputs 1 if it is not possible to complete.

input and Output sample

Enter Sample #:

5 7 11 2 53 1 42 4 83 2 35 2 93 4 74 5-6

Output Sample #:

4

Two-point answer, check can be, need to spend money on the number of roads, running the shortest path algorithm.

#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <
Queue> using namespace std;
const int n=1005;
int n,m,k,cnt,hd[n],dis[n];
BOOL Inq[n];
queue<int>q; struct Edge {int to,nxt,val;}
V[20005];
	void Addedge (int x,int y,int z) {++cnt;
	V[cnt].to=y;
	V[cnt].val=z;
	V[CNT].NXT=HD[X];
hd[x]=cnt;
	BOOL Bin (int lim) {memset (dis,0x3f,sizeof (dis));
	dis[1]=0;
	Inq[1]=1;
	Q.push (1);
		while (!q.empty ()) {int U=q.front ();
		Q.pop ();
		inq[u]=0;
					for (int i=hd[u];i;i=v[i].nxt) if (V[i].val<=lim) {if (dis[v[i].to]>dis[u)) {Dis[v[i].to]=dis[u];
						if (!inq[v[i].to]) {inq[v[i].to]=1;
					Q.push (v[i].to);
					}} else {if (dis[v[i].to]>dis[u]+1) {dis[v[i].to]=dis[u]+1;
						if (!inq[v[i].to]) {inq[v[i].to]=1;
					Q.push (v[i].to);
	}} if (dis[n]<=k) return 1;
return 0; int main () {scanf ("%d%d%d", &n,&m,&k);
	int l=0,r=0,mid;
		for (int i=1;i<=m;i++) {int x,y,z;
		scanf ("%d%d%d", &x,&y,&z);
		Addedge (X,Y,Z);
		Addedge (Y,X,Z);
	R=max (R,Z);
		while (L<r) {mid= (l+r)/2;
		if (Bin (mid)) R=mid;
	else l=mid+1;
	} if (Bin (l)) printf ("%d\n", L);
	else printf (" -1\n");
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.