bzoj4681 [Jsoi2010] Travel

Source: Internet
Author: User

[Jsoi2010] Travel time limit:30 Sec Memory limit:256 mbdescription

Wjj like to travel, this time she intends to go to a valley that is said to have a lot of beautiful waterfalls to play.
WJJ obtained a map in advance, labeled N (1< = n< = 50) Small Animal colony, that is, a small village. One of the 1th villages
Zhuang is the place where wjj now lives, and the nth village is where WJJ intends to go. Between these villages there is M (1< = m< = 150) Two-way road connection, section J
The two-way road is directly connected to two small villages A, B, the length is C (1< = b < = n,ai<>b, 1< = c< = 1000). Roads have tunnels.
Road, there are trestle, on the map those seemingly crossing the village outside the road does not actually intersect-that is, if these small villages and two-way
The road network of the road is regarded as a graph in the sense of graph theory, we do not guarantee that it is a plan, nor does it guarantee that it has no heavy edges. However, one thing that can be guaranteed
WJJ carefully verified that the village from which it resides must be able to walk to the valley where she wants to go.
In the magical world where WJJ is located, every small animal can use a cactus to cast magic, one of which is to exchange any two bidirectional roads in the world
Length, while keeping the length of other roads unchanged. By WJJ current level of magic, she can use K (1< = k< = 20) times this road length intersection
Change the magic. Unfortunately, because the Cactus thorn is more, WJJ does not intend to travel with it, so she will be at home to complete the desired road exchange and then out
Door. Suppose there were no other small animals on Wji's journey to disrupt the route she had planned. To reach its destination as soon as possible, Wjj hoped she
The shorter the total distance you need to go, the better. In other words, what is the shortest distance from village 1 to village N when using up to K magic?

Input

Line 1th: 3 integers separated by a space n,m,k
2nd.. M+1 Line: Each line is 3 integers, separated by a space, respectively, a, b C

Output

1 integers representing the shortest distance between the village 1 and the village n after using a maximum of K magic.

Sample Input

5 5 2

1 2 10

2 5 10

1 3 4

5 | |

4 5 1

Sample Output

3

One possible option is to swap the length of the 1th and 4th edges, and then the length of the 1th and 5th edges. The shortest path after swapping is 1-->2-

->5, Length is 3. Obviously, there is no better solution than this.


This problem is very good ah ... I don't even know if he's a DP or a graph-theory.
The optimal solution should be to sort the edges in front of each other, and then in the back side of the color ....
So let's enumerate what we've been using next to each other ...
Sort all edges according to Benquan, and if you use a 01 string to indicate whether each edge is being used, then the 01 strings after the swap edge are a front all 1, followed by 0 with 1 strings. So we enumerate where this dividing line L is. For each path, we can choose to go directly to the past, or an exchange weight value with the front L bar. Then consider using f[i][j][k] to indicate to the point I, the front L bar with J, exchange the minimum price k times. There are three cases of transfer:

   1、这条边是前L条边中的一条。f[arr[e]][j+1][k]=min(f[arr[e]][j+1][k],f[i][j][k]+weight[j+1]) 这是因为边e和边j+1都是一定会被用到的,先用哪个无所谓,但是为了方便转移前L条边只能被从小到大使用。    2、这条边不在前L条边中。          1)f[arr[e]][j][k]=min(f[arr[e]][j][k],f[i][j][k]+weight[e]) 直接使用边e          2)f[arr[e]][j+1][k+1]=min(f[arr[e]][j+1][k+1],f[i][j][k]+weight[j+1]) 将边e和前L条边中的一条交换。

Then use the shortest circuit to maintain this thing ...



#include <bits/stdc++.h>using namespaceStdConst intN = the, M =305;structlpl{intp[2], Len;} Lin, edge[m];structld{intI, J, K;};intN, M, K, cnt, ans =0x7fffffff, Dis[n][m][n];BOOLvis[n][m][n];vector<int> point[n];queue<ld> q;inline BOOLCMP (LPL A, LPL B) {returnA.len < B.len;}inline voidWORKK (intL) {LD now;intII, JJ, KK, Qwe; memset (Vis,false,sizeof(VIS)); memset (DIS,0x3f,sizeof(dis)); dis[1][0][0] =0; Q.push ((LD) {1,0,0}); while(!q.empty ()) {now = Q.front (); Q.pop (); ii = now.i; JJ = now.j; kk = NOW.K; Vis[ii][jj][kk] =false; for(inti = point[ii].size ()-1; I >=0; -i) {qwe = Point[ii][i];intto = Edge[qwe].p[ii = = edge[qwe].p[0]];if(Qwe <= L) {if(JJ < L && DIS[TO][JJ +1][KK] > Dis[ii][jj][kk] + edge[jj +1].len) {DIS[TO][JJ +1][KK] = Dis[ii][jj][kk] + EDGE[JJ +1].len;if(!VIS[TO][JJ +1][KK]) {VIS[TO][JJ +1][KK] =true; Q.push (LD) {to, JJ +1, KK}); }                }            }Else{if(JJ < L && KK < k && DIS[TO][JJ +1][kk +1] > Dis[ii][jj][kk] + edge[jj +1].len) {DIS[TO][JJ +1][kk +1] = Dis[ii][jj][kk] + EDGE[JJ +1].len;if(!VIS[TO][JJ +1][kk +1]) {VIS[TO][JJ +1][kk +1] =true; Q.push (LD) {to, JJ +1, KK +1}); }                }if(Dis[to][jj][kk] > Dis[ii][jj][kk] + edge[qwe].len) {DIS[TO][JJ][KK] = Dis[ii][jj][kk] + Edge[qwe].len;if(!vis[to][jj][kk]) {VIS[TO][JJ][KK] =true;                    Q.push (LD) {To, JJ, KK}); }                }            }        }    } for(inti =0; I <= K; ++i) ans = min (ans, dis[n][l][i]);}intMain () {//freopen ("data.in", "R", stdin);    //freopen ("Lpl.out", "w", stdout);scanf"%d%d%d", &n, &m, &k); for(inti =1; I <= m; ++i) {scanf ("%d%d%d", &lin.p[0], &lin.p[1], &lin.len); cnt++;    EDGE[CNT] = Lin; } Sort (Edge +1, Edge +1+ M, CMP); for(inti =1; I <= m; ++i) {point[edge[i].p[0]].push_back (i); point[edge[i].p[1]].push_back (i); } for(inti =0; I <= m;    ++i) Workk (i); cout << ans;return 0;}

bzoj4681 [Jsoi2010] Travel

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.