POJ 3662 Telephone Lines binary answer + djk

Source: Internet
Author: User

[Cpp]/* POJ 3662 Telephone Lines: Build a cable from 1 to N. A pair of p pairs can be connected to the pole. A telecommunications company can provide k cables, the others are provided by John, and there should be many solutions to find the longest (ret) length of the cable provided by john, but there is another condition, ask the smallest ret in all the solutions, so let the telecommunications company provide the k and the long one, the rest are provided by john, and then pick out the longest (length L ), that is to say, as long as the cable is longer than L and used on this road, it should be provided by telecom companies. Now assume that john provides the longest cable length of L and finds a path from 1 to N, so that the number of cables with a length greater than L on this road cannot exceed k (how can this number be calculated? The edge weight of a cable with a length greater than L is 1, The Edge Weight of the cable provided by john is 0, and then the shortest short circuit is). That is a solution, you only need to make the L smaller as much as possible (the best value can be obtained after the binary length) */# include <stdio. h> # include <string. h ># include <algorithm> using namespace std; struct edge {int a, B, l;} bian [10100]; int map [1010] [1010]; int vis [1010]; int dist [1010]; int inf = 0x7ffffff; bool cmp (struct edge a, struct edge B) {return. l <B. l;} void build (int l, int n, int p) {int I, j; for (I = 0; I <= n; ++ I) {for (j = 0; j <= n; ++ J) {map [I] [j] = inf ;}for (I = 0; I <p; ++ I) {int a, B; a = bian [I]. a; B = bian [I]. b; if (bian [I]. l <= l) {map [a] [B] = map [B] [a] = 0 ;} else map [a] [B] = map [B] [a] = 1 ;}} int xun (int n) // find the nearest {int xia =-1, I; for (I = 1; I <= n; I ++) in the remaining nodes) if (vis [I] = 0 & (xia =-1 | dist [I] <dist [xia]) xia = I; return xia ;} int djk (int n) {int I, j, just; for (I = 1; I <= n; I ++) // initialize dist [I] = map [1] [I]; memset (vis, 0, sizeof (vis); vis [1] = 1; for (I = 2; I <= n; I ++) {just = xun (n); vis [just] = 1; // just closest, mark it as the shortest path found for (j = 1; j <= n; j ++) // update the remaining if (vis [j] = 0 & dist [j]> dist [just] + map [just] [j]) dist [j] = dist [just] + map [just] [j];} return dist [n];} int bin (int n, int p, int k) {int head, tail; head = 0; tail = p-1; int mid; while (head <= tail) {int mid = (head + tail)> 1; build (bian [mid]. l, n, p); if (djk (n) <= k) // find the smallest {tail = mid-1;} else head = mid + 1 ;} return head;} int ma In () {int I, n, p, k; while (scanf ("% d", & n, & p, & k )! = EOF) {for (I = 0; I <p; I ++) {scanf ("% d", & bian [I]. a, & bian [I]. b, & bian [I]. l);} sort (bian, bian + p, cmp); build (0, n, p); int duan = djk (n); if (duan = inf) {printf ("-1 \ n");} else {if (duan <= k) printf ("0 \ n"); else printf ("% d \ n ", bian [bin (n, p, k)]. l) ;}} 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.