Bzoj 1097: [POI2007] Tourist attractions ATR (shortest circuit + pressure DP)

Source: Internet
Author: User

First, the shortest-circuit pretreatment, then the pressure on the line

--------------------------------------------------------------------------

#include <cstdio>#include <cstring>#include <algorithm>#include <cctype>#include <queue>using namespace std; 

#define B (i) (1 << (i))

typedef pair<int, int> PII;inline int Read () {char C = getchar ();int ret = 0;For (;!isdigit (c); c = GetChar ());For (; IsDigit (c); c = GetChar ()) ret = RET * + C-' 0 ';return ret;}const int MAXN = 20009;const int MAXK = +;const int INF = 0X3F3F3F3F;bool VIS[MAXN];int F[MAXN], _F[MAXN];int dp[b (MAXK)][maxk];int N, K;struct Edge {int to, W;edge* Next;} e[400009], *pt = E, *HEAD[MAXN];void Addedge (int u, int v, int w) {pt->to = v; pt->w = w; pt->next = Head[u]; Head[u] = pt++;}struct DIJKSTRA {int D[MAXN];Priority_queue<pii, Vector<pii>, greater<pii> > Q;void work (int s) {for (int i = 0; i < N; i++) d[i] = INF;Q.push (Make_pair (D[s] = 0, s));While (!q.empty ()) {PII o = q.top (); Q.pop ();int w = o.first, x = O.second;if (d[x]! = W) continue;For (edge* e = head[x]; e; e = e->next) if (D[e->to] > D[x] + e->w)Q.push (Make_pair (d[e->to] = d[x] + e->w, e->to));}}} D[MAXK];int Dp (int s, int x) {int &ans = dp[s][x];if (~ans) return ans;ans = INF;if ((_f[x] | s)! = s) return ans;for (int i = 0; i < K; i++) if (x! = I && (S & B (i)) &&! ( F[X] & B (i)))ans = min (Dp (s ^ b (x), i) + d[i].d[x + 1], ans);return ans;}void Dfs (int x) {if (vis[x]) return;vis[x] = true;for (int i = 0; i < K; i++) if (F[x] & B (i)) {DFS (i);F[x] |= f[i];}}void Dfs (int x) {if (vis[x]) return;vis[x] = true;for (int i = 0; i < K; i++) if (_f[x] & B (i)) {Dfs (i);_f[x] |= _f[i];}}void Init () {N = read ();int m = read ();K = read ();While (m--) {int u = Read ()-1, v = Read ()-1, W = read ();Addedge (U, V, W);Addedge (V, U, W);}memset (f, 0, sizeof f);memset (_f, 0, sizeof _f);m = read ();While (m--) {int u = Read ()-2, V = Read ()-2;F[u] |= B (v);_f[v] |= B (u);}for (int i = 0; i < K; i++)D[i]. Work (i + 1);memset (Vis, 0, sizeof vis);for (int i = 0; i < K; i++)if (!_f[i]) DFS (i);memset (Vis, 0, sizeof vis);for (int i = 0; i < K; i++)if (! F[i]) Dfs (i);}int main () {Init ();if (! K) {D[0]. Work (0);printf ("%d\n", d[0].d[n-1]);return 0;}memset (DP,-1, sizeof DP);for (int i = 0; i < K; i++) {dp[b (i)][i] = d[i].d[0];if (_f[i]) dp[b (i)][i] = INF;}int ans = INF;for (int i = 0; i < K; i++) if (! F[i])ans = min (ans, Dp (b (K)-1, i) + d[i].d[n-1]);printf ("%d\n", ans);return 0;}

--------------------------------------------------------------------------

1097: [POI2007] Tourist attractions ATR time limit: $ Sec Memory Limit: 357 MB
Submit: 1370 Solved: 298
[Submit] [Status] [Discuss] Description

FGD wants to travel from Chengdu to Shanghai. On the road he wants to go through some cities and enjoy the scenery, taste the delicacies or do other interesting things. The order of these cities is not entirely arbitrary, for example, FGD does not want to go to the next city to climb after a big meal, but wants to go somewhere else to have afternoon tea. Fortunately, the FGD journey was not established and he was able to choose between certain travel options. Since FGD hated the bumps in the ride, he hoped to meet his requirements and travel as short as possible, so that he had enough energy to enjoy the scenery or the ^_^ of the 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 so is the road. Without direct access to its own road from a city, there is a maximum of two cities connected directly to one road, but there can be multiple routes connecting two cities. If any two roads meet, then the meeting point is also bound to be one of the N cities, halfway through, because of the construction of overpass and under the tunnel, the road will 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 is numbered 1, Shanghai is numbered n, and the number of N cities that FGD wants to pass is 2,3,..., k+1. For example, suppose a traffic network is like. FGD wants to go through the city 2,3,4,5, and stops at 2 before 3, while 4,5 stops at 3. The shortest travel option is 1-2-4-3-4-5-8, with a total length of 19. 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, this solution is 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.

Source

Bzoj 1097: [POI2007] Tourist attractions ATR (shortest circuit + pressure DP)

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.