Victor and World
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 2238 Accepted Submission(s): 1037
Problem Description
After trying hard for many years, Victor has finally received a pilot license. To have a celebration, he intends to buy himself an airplane and fly around the world. There are n countries on the earth, which are numbered from 1 to n. They are connected by m undirected flights, detailedly the i-th flight connects the ui-th and the vi-th country, and it will cost Victor's airplane wi L fuel if Victor flies through it. And it is possible for him to fly to every country from the first country.
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
Source
Recommend
hujie | We have carefully selected several similar problems for you: 6645 6644 6643 6642 6641
题目给出的数据 n < 16。很明显不要我们用太复杂的图论求解,而且正好开的下二维数组,可以用状压dp了
dp【s】【i】(当前经过了s,在点i 所用的权值)中 s是个集合,i表示当前所在的点。
s用的是十进制来表示的二进制。如:s = 3 = 101 表示当前集合中点1和点3已经经过,点2没有经过。
还有要注意的就是本题的TSP是可以跑回路的,一个点可以经过多次。我们在处理之前跑一便最短路(Floyd)。
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <ctype.h>
#include <bitset>
#define LL long long
#define ULL unsigned long long
#define mod 10007
#define INF 0x7ffffff
#define mem(a,b) memset(a,b,sizeof(a))
#define MODD(a,b) (((a%b)+b)%b)
#define maxn 50
using namespace std;
//const int maxn = 50;
int mp[maxn][maxn];
int n;
int dp[(1 << 16) + 10][maxn];
void init()
{
fill(mp[0],mp[0] + maxn * maxn,INF);
fill(dp[0],dp[0] + (1 << 16) * maxn,INF);
for(int i = 0; i < maxn; i++) mp[i][i] = 0;
//fill(mp,mp + 50 * 50,INF);
}
void Floyd()
{
for(int k = 0; k < n;k++){
for(int i = 0; i < n;i++){
for(int j = 0; j < n; j++){
mp[i][j] = min(mp[i][k] + mp[k][j],mp[i][j]);
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
int m;
init();
scanf("%d%d",&n,&m);
while(m--){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
a--,b--;
if(mp[a][b] > c) mp[a][b] = mp[b][a] = c;
}
Floyd();
dp[1][0] = 0;
for(int s = 1; s < (1 << n); s++){//考虑所有出现的情况
for(int i = 0; i < n; i++)//遍历两点之间的起点
if(s & (1 << i)){
for(int j = 0; j < n; j++){//遍历终点
if((s & (1 << j)) == 0){
dp[s | (1 << j)][j] = min(dp[s | (1 << j)][j],dp[s][i] + mp[i][j]);
}
}
}
}
int ans = INF;
for(int i = 1; i < n; i++){//这一步是回到起点,遍历所有的点(因为所有的点都肯能是最后一个点),选一个最小的的边。
ans = min(ans,dp[(1 << n) - 1][i] + mp[i][0]);
}
if(n == 1) printf("0\n");
else printf("%d\n",ans);
}
return 0;
}