BZOJ1097: [POI2007] Tourist Attractions ATR

Source: Internet
Author: User

.. K times the shortest, consider how to meet first go some points

Use the pressure DP, each point to consider the point it needs to pass A[i], when the current traversed by the point contains a[i], I this point can be reached.

Write with a memory search.

1#include <bits/stdc++.h>2 #definell Long Long3 using namespacestd;4InlineintRead () {5   intx=0, f=1;CharCh=GetChar ();6    while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}7    while(ch>='0'&&ch<='9') {x=Ten*x+ch-'0'; ch=GetChar ();}8   returnx*F;9 }Ten #defineM 200005 One #defineN 20005 A #defineINF 1e9 - structnode{ -   intto,next,v; the}e[m<<1]; - intn,m,k,tot,head[n],d[ -][n],a[ -],dp[ -][2100000]; - BOOLVis[n]; - voidAddintXintYintz) { +E[++tot]= (Node) {y,head[x],z};head[x]=tot; -E[++tot]= (Node) {x,head[y],z};head[y]=tot; + } Aqueue<int>Q; at voidSPFA (intx) { -    for(intI=1; i<=n;i++) d[x][i]=inf; -d[x][x]=0; vis[x]=1; Q.push (x); -    while(!Q.empty ()) { -     intNow=q.front (); Q.pop (); vis[now]=0; -      for(intI=head[now];i;i=e[i].next) in       if(d[x][now]+e[i].v<D[x][e[i].to]) { -d[x][e[i].to]=d[x][now]+e[i].v; to         if(!Vis[e[i].to]) { +vis[e[i].to]=1; Q.push (e[i].to); -         } the       } *   } $ }Panax Notoginseng intCalcintx) { -   intC=0; the    while(x) { +     if(x&1) C + +; Ax>>=1; the   } +   returnC; - } $ intDfsintXinty) { $   if(dp[x][y]>=0)returnDp[x][y]; -   if(y== (1&LT;&LT;K)-1)returnd[x+1][n]; -dp[x][y]=inf; the    for(intI=1; i<=k;i++) -     if((y|a[i]) = =y)WuyiDp[x][y]=min (dp[x][y],d[x+1][i+1]+dfs (i,y| (1<< (I-1))) ); the   returnDp[x][y]; - } Wu intMain () { -  //freopen ("1097.in", "R", stdin); AboutN=read (); M=read (); k=read (); $    for(intI=1; i<=m;i++){ -     intX=read (), Y=read (), z=read (); - Add (x, y, z); -   } A    for(intI=1; i<=k+1; i++) SPFA (i); +memset (dp,-1,sizeof(DP)); the   intt=read (); -    while(t--){ $     intX=read ()-1, Y=read ()-1; thea[y]|=1<< (x1); the   } theprintf"%d\n", DFS (0,0)); the   return 0; -}
View Code

1097: [POI2007] Tourist attractions ATR time limit:30 Sec Memory limit:357 MB
submit:1574 solved:363
[Submit] [Status] [Discuss] Description

FGD wants to travel from Chengdu to Shanghai. On the road he hopes to pass through some cities and enjoy the scenery, taste the delicacies or do other interesting
of things. The order of these cities is not entirely arbitrary, for example, FGD does not want to go to the next city to climb the mountain immediately after having eaten a big meal,
But I want to go somewhere else for afternoon tea. Fortunately, the FGD journey was not established and he was able to choose between certain travel options. Because
FGD hated the bumps in the ride, and he wanted to travel as short as possible to meet his requirements, so he had enough energy to appreciate the wind.
Scene or bubble mm ^_^. The entire urban transport network consists of N cities and two-way road m between cities and city. The city is numbered from 1 to N, and the road
Road. Without direct access to its own road from a city, there is a maximum of one road connecting two cities, but there can be more than two
The path of the city. If any two roads meet, then the meeting point is also bound to be one of the N cities, halfway through, due to the construction of overpass and under tunnel
, the road does not intersect. Each road has a fixed length. In the middle of the journey, FGD wants to go through K (k<=n-2) cities. Chengdu No. 1, Shanghai
Number is n, and the number of N cities that FGD wants to pass is 2,3,..., k+1. For example, suppose that a traffic network is like. FGD wants to go through the city 2,3,
4,5, and at 2 stops before 3, while 4,5 stays after 3. So the shortest travel option is 1-2-4-3-4-5-8, with a total length of 1
9. Note the FGD in order to from the City 2 to the City 4 can pass the City 3, but not in the City 3 stay. This will not violate the requirements of FGD. And because FGD wants to
Take the shortest path, so this solution is exactly what FGD needs.

Input

The first line contains 3 integers N (2<=n<=20000), M (1<=m<=200000), K (0<=k<=20), meaning as described above.

Output

Contains only one row, containing an integer that represents the shortest travel distance.

Sample Input8 15 4
1 2 3
1 3 4
1 4 4
1 6 2
1 7 3
2 3 6
2 4 2
2 5 2
3 4 3
3 6 3
3 8 6
4 5 2
4 8 6
5 7 4
5 8 6
3
2 3
3 4
3 5Sample Output19

HINT

The above corresponds to the example given in the topic.

BZOJ1097: [POI2007] Tourist Attractions ATR

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.