Travelling Time limit:6000/3000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 7476 accepted Submission (s): 2434
Problem Description after coding so many DAYS,MR Acmer wants to have a good rest. So travelling is the best choice! He has decided to visit N cities (him insists on seeing all the cities! And he does not mind which city being the his start station because Superman can bring him to all city at the A-but only once. ), and of course there are M roads here,following a fee as usual. But Mr Acmer gets bored so easily the he doesn ' t want to visit a city more than twice! And he, so mean, he wants to minimize the total fee! He's lazy you. So him turns to your for help.
Input There are several test cases,the a-i-two intergers n (1<=n<=10) and M,which means he-needs to visit n Cities and there are M roads he can choose,then m lines Follow,each line would include three intergers a,b and C (1<=a,b <=n), means there is a road between A and B and the cost is of course to the "end of".
Output output the minimum fee that him should pay,or-1 if he can ' t find such a route.
Sample Input
2 1 1 2 100 3 2 1 2 40 2 3 50 3 3 1 2 3 1 3 4 2 3-10
Sample Output
100 90 7
Source 2009 Multi-university Training Contest 11-host by Hrbeu
Topic Link: https://vjudge.net/problem/POJ-3254
A person to travel vacation, give n points, M strip right without the edge,
Ask the person for the minimum cost of each point.
The starting point is any point, but it can only go through 2 times per point.
It's about the same as POJ 3311 Hie with the Pie POJ, but here's a maximum of 2 times per point.
So the state is stored in 3, 0 1 2, respectively, after the point 0, 1 2 times
Dp[s][i] represents the minimum cost of an I point under State S (after 1 or 2 times)
Dp[s][i] = min{dp[s][i], Dp[s-san[i-1]][j] + dis[j][i]}
J is the point that passes through in the state S and J!= I
And the technique of taking the state S's 3-bit I-position is
(S/san[i-1])% 3, san[i-1] is 3^ (i-1)
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <
queue> #include <string> #define LL long #define INF 0x3f3f3f3f #define INF 1e18 using namespace std;
typedef pair<int, int> P;
const int MAXN = 6e4 + 5;
const int mod = 1E8;
int n, m;
int dp[maxn][11];
int dis[11][11]; int san[11]; The topic asks after each point not more than 2 times, therefore uses 3 to save the state//0 1 2 to indicate the number of passes through the point int main (void) {//Std::ios::sync_with_stdio (FALSE), Cin.tie (0), Cout.tie
(0);
Freopen ("In.txt", "R", stdin);
San[0] = 1;
for (int i = 1; I <= i++) san[i] = san[i-1] * 3;
int U, V, W;
while (~SCANF ("%d%d", &n, &m)) {memset (DIS, inf, sizeof dis);
for (int i = 1; I <= m i++) {scanf ("%d%d%d", &u, &v, &w);
Dis[u][v] = min (Dis[u][v], W);
Dis[v][u] = min (Dis[v][u], W);
int ans = inf, passall; for (int S = 0; S < san[n];
s++) {passall = 1; for (int i = 1; I <= n; i++) {//Determine whether the current state has passed all dots int cc = (s/sAN[I-1])% 3;
Gets the I-1 bit if (cc = = 0) Passall = 0 of the third binary of S;
for (int i = 1; I <= n; i++) {int c = (s/san[i-1])% 3; if (c = = 0) continue; If S has a i-1 bit of three to 0, it does not pass through the point I if (s = = San[i-1] | | S = = san[i-1] * 2) dp[s][i] = 0;
If S passes point I, 1 times or 2 times, the cost is initialized to 0 else {dp[s][i] = inf;
for (int j = 1; J <= N; j +) {int C2 = (s/san[j-1])% 3;
if (C2 && i!= j) dp[s][i] = min (dp[s][i), Dp[s-san[i-1]][j] + dis[j][i]); } if (passall) ans = min (ans, dp[s][i]);
Current s passes all points}} if (ans = = inf) puts ("-1");
else printf ("%d\n", ans);
return 0;
}