hdu 5418 Victor and World
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5418
Victor now is at the country whose number is 1, he wants to know the minimal amount of fuel for him to visit every country at least once and finally return to the first country. Input The first line of the input contains an integer T, denoting the number of test cases.
In every test case, there are two integers n and m in the first line, denoting the number of the countries and the number of the flights.
Then there are m lines, each line contains three integers ui, vi and wi, describing a flight.
1≤T≤20.
1≤n≤16.
1≤m≤100000.
1≤wi≤100.
1≤ui,vi≤n. Output Your program should print T lines : the i-th of these should contain a single integer, denoting the minimal amount of fuel for Victor to finish the travel. Sample Input 1 3 2 1 2 2 1 3 3 Sample Output 10
题意:有n个城市,在n个城市之间有m条双向路,每条路有一个距离,现在问从1号城市去游览其它的2到n号城市最后回到1号城市的最短路径(保证1可以直接或间接到达2到n)。(n<=16)
题解:状压DP,
#include <bits/stdc++.h> using namespace std; const int maxn = 20, maxm = 8e4; int mp[maxn][maxn], dp[maxm][maxn]; int main(){ int T; scanf("%d",&T); while(T--){ int n, m, ans = 100000008; scanf("%d%d",&n,&m); memset(mp, 0x3f, sizeof(mp)); for(int i = 1; i <= m; i++){ int u,v,w; scanf("%d%d%d",&u,&v,&w); mp[u][v] = mp[v][u] = min(mp[u][v], w);//! } for(int i = 1; i <= n; i++)mp[i][i] = 0; for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(i != j) mp[i][j] = min(mp[i][k] + mp[k][j], mp[i][j]); memset(dp, 0x3f, sizeof(dp)); dp[1][1] = 0; for(int s = 0; s <= (1<<n) - 1; s++) for(int i = 1; i <= n; i++) for(int k = 1; k <= n; k++){ if(!(s & (1<<(i-1))))continue; if(s & (1<<(k-1)))dp[s][i] = min(dp[s^(1<<(i-1))][k] + mp[k][i], dp[s][i]); } for(int i = 1; i <= n; i++)ans = min(ans , dp[(1<<n)-1][i] + mp[i][1]); printf("%d\n",ans); } }View Code
标签:hdu,int,country,5418,th,World,first,Victor From: https://www.cnblogs.com/EdSheeran/p/8873643.html