日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【HDU - 5418】Victor and World(tsp旅行商问题,状压dp,floyd最短路,图论)

發布時間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【HDU - 5418】Victor and World(tsp旅行商问题,状压dp,floyd最短路,图论) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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?nn?countries on the earth, which are numbered from?11?to?nn. They are connected by?mm?undirected flights, detailedly the?ii-th flight connects the?uiui-th and the?vivi-th country, and it will cost Victor's airplane?wiwi?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?11, 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?TT, denoting the number of test cases.?
In every test case, there are two integers?nn?and?mm?in the first line, denoting the number of the countries and the number of the flights.?

Then there are?mm?lines, each line contains three integers?uiui,?vivi?and?wiwi, describing a flight.?

1≤T≤201≤T≤20.?

1≤n≤161≤n≤16.?

1≤m≤1000001≤m≤100000.?

1≤wi≤1001≤wi≤100.?

1≤ui,vi≤n1≤ui,vi≤n.

Output

Your program should print?TT?lines : the?ii-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可以直接或間接到達其他城市)。(n<=16)

解題報告:

因為每個點可以走多次所以不是哈密頓回路,這一個特性也就決定了我們可以用floyd求最短路。

dp[sta][v]代表走過狀態為sta并且當前站在v這個城市的最小距離。最后加一個回到原點的距離就行了。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; int n,m; int head[MAX],tot; int dis[22][22]; int dp[1<<18][18]; bool ok[22]; struct Edge {int fr,to,ne;int w; } e[MAX]; void add(int u,int v,int w) {e[++tot].fr = u;e[tot].to = v;e[tot].w = w;e[tot].ne = head[u];head[u] = tot; } void floyd() {for(int k = 0; k<n; k++) {for(int i = 0; i<n; i++) {for(int j = 0; j<n; j++) {dis[i][j] = min(dis[i][j],dis[i][k] + dis[k][j]);}}} } int tsp() {int up = 1<<n;dp[1][0] = 0; for(int sta = 0; sta < up; sta++) {memset(ok,0,sizeof ok);for(int i = 0; i<n; i++) {if((1<<i) & sta) ok[i] = 1;}for(int u = 0; u<n; u++) { //可以if(dp==-1)continue 也可以先初始化成0x3f3f3f3f??? for(int v = 0; v<n; v++) {if(ok[v]) continue;dp[sta|(1<<v)][v] = min(dp[sta|(1<<v)][v],dp[sta][u] + dis[u][v]);}}}int ans = 0x3f3f3f3f;for(int i = 0; i<n; i++) {ans = min(ans,dp[up-1][i] + dis[i][0]);}return ans; } int main() {int t;cin>>t;while(t--) {scanf("%d%d",&n,&m);//init tot=0;memset(dis,0x3f,sizeof dis);memset(dp,0x3f,sizeof dp);for(int i = 0; i<n; i++) head[i] = -1,dis[i][i] = 0;for(int u,v,w,i = 1; i<=m; i++) {scanf("%d%d%d",&u,&v,&w);u--,v--;add(u,v,w);add(v,u,w);if(w < dis[u][v]) dis[u][v] = dis[v][u] = w;}floyd();cout << tsp() << endl;}return 0 ; }

?

總結

以上是生活随笔為你收集整理的【HDU - 5418】Victor and World(tsp旅行商问题,状压dp,floyd最短路,图论)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。