Rogu P2939 [usaco 09feb] rebuild Revamping Trails, p2939revamping

Source: Internet
Author: User

Rogu P2939 [usaco 09feb] rebuild Revamping Trails, p2939revamping
Description

Farmer John dutifully checks on the cows every day. he traverses some of the M (1 <= M <= 50,000) trails conveniently numbered 1 .. M from pasture 1 all the way out to pasture N (a journey which is always possible for trail maps given in the test data ). the N (1 <= N <= 10,000) pastures conveniently numbered 1 .. N on Farmer John's farm are currently connected by bidirectional dirt trails. each trail I connects pastures P1_ I and P2_ I (1 <= P1_ I <= N; 1 <= P2_ I <= N) and requires T_ I (1 <= T_ I <= 1,000,000) units of time to traverse.

He wants to revamp some of the trails on his farm to save time on his long journey. specifically, he will choose K (1 <= K <= 20) trails to turn into highways, which will adjust tively reduce the trail's traversal time to 0. help FJ decide which trails to revamp to minimize the resulting time of getting from pasture 1 to N.

Time limit: 2 seconds

John has N pastures in total. The trails are connected by M Dusty Trails. The trails are accessible in two directions. John leaves from farm 1 every morning to farm N to check his health for the cows.

It takes some time to pass each trail. john planned to upgrade K of the trails to make them high-speed roads. the highway traffic is almost completed in an instant, so the highway traffic time is 0.

Please help john decide which trails to upgrade, so that he spends the least time to reach the farm every morning. output the minimum time.

Input/Output Format

Input Format:

 

* Line 1: Three space-separated integers: N, M, and K

* Lines 2.. M + 1: Line I + 1 describes trail I with three space-separated integers: P1_ I, P2_ I, and T_ I

 

Output Format:

 

* Line 1: The length of the shortest path after revamping no more than K edges

 

Input and Output sample input sample #1: Copy
4 4 1 1 2 10 2 4 10 1 3 1 3 4 100 
Output example #1: Copy
1 
Description

K is 1; revamp trail 3-> 4 to take time 0 instead of 100. The new shortest path is 1-> 3-> 4, total traversal time now 1.

 

If you know what a layered graph is, it's a raw question.

Otherwise, it is a question.

First, it is definitely not easy to process the source image.

Then we layer the graph. Specifically, we create K + 1 source image. The Edge Weight between the graph and the graph is 0, and the edge is the edge in the source image.

In this way, you can run a two-dimensional dijstra (hard to write ).

 

# Include <cstdio> # include <algorithm> # include <queue> # include <cstring> # define Pair pair <int, int> # define F first # define S secondconst int MAXN = 1e6 + 10; using namespace std; inline int read () {char c = getchar (); int x = 0, f = 1; while (c <'0' | c> '9') {if (c = '-') f =-1; c = getchar () ;}while (c >='0' & c <= '9') {x = x * 10 + c-'0 '; c = getchar ();} return x * f;} int N, M, K; int dis [MAXN] [21], vis [MAXN] [21]; struct node {int u, v, w, nxt;} edge [M AXN]; int head [MAXN], num = 1; inline void AddEdge (int x, int y, int z) {edge [num]. u = x; edge [num]. v = y; edge [num]. w = z; edge [num]. nxt = head [x]; head [x] = num ++;} void Dijstra () {memset (dis, 0xf, sizeof (dis )); dis [1] [0] = 0; // vertex I, layer j priority_queue <pair <int, Pair> q; q. push (make_pair (0, make_pair (); // The first vertex represents the second presentation layer while (q. size ()! = 0) {while (vis [q. top (). s. f] [q. top (). s.S] & q. size ()> 0) q. pop (); Pair p = q. top (). second; vis [p. f] [p.S] = 1; for (int I = head [p. f]; I! =-1; I = edge [I]. nxt) {int will = edge [I]. v; if (vis [will] [p.S] = 0 & dis [will] [p.S]> dis [p. f] [p.S] + edge [I]. w) dis [will] [p.S] = dis [p. f] [p.S] + edge [I]. w, q. push (make_pair (-dis [will] [p.S], make_pair (will, p.S ))); if (p.S + 1 <= K & vis [will] [p.S + 1] = 0 & dis [will] [p.S + 1]> dis [p. f] [p.S]) dis [will] [p.S + 1] = dis [p. f] [p.S], q. push (make_pair (-dis [will] [p.S + 1], make_pair (will, p.S + 1) ;}} printf ("% d ", dis [N] [K]);} int main () {memset (head,-1, sizeof (head); N = read (); M = read (); K = read (); for (int I = 1; I <= M; I ++) {int x = read (), y = read (), z = read (); AddEdge (x, y, z); AddEdge (y, x, z);} Dijstra (); 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.