hdu5418Victor and World 状压最短路

    xiaoxiao2022-07-14  153

    Victor and World

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others) Total Submission(s): 2125    Accepted Submission(s): 982  

    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

    BestCoder Round #52 (div.2)

     

    状压最短路,跟状压bfs的思想一样,开另外一维(1 << 17),来记录当前走过的点的状压

    int f[20][1 << 17]; int vis[20][1 << 17]; int dis[20][20]; int spfa() { queue<P>q; q.push(P(0,0)); f[0][0] = 0; vis[0][0] = 1; while(!q.empty()){ int x = q.front().fi; int y = q.front().se; // wt(x); q.pop(); vis[x][y] = 0; for(int i = 0;i < n;i++){ int s = y | (1 << i); if(dis[x][i] != 1e9 && f[x][y] + dis[x][i] < f[i][s]){ f[i][s] = dis[x][i] + f[x][y]; if(!vis[i][s]){ q.push(P(i,s)); vis[i][s] = 1; } } } } return f[0][(1 << n) - 1]; } int main() { int t,m; ios; cin >> t; while(t--){ cin >> n >> m; MS0(vis); memset(f,INF,sizeof(f)); memset(dis,INF,sizeof(dis)); for(int i = 0;i < n;i++) dis[i][i] = 0; for(int i = 1;i <= m;i++){ int x,y,z;cin >> x >> y >> z; x--,y--; dis[x][y] = min(dis[x][y],z); dis[y][x] = min(dis[y][x],z); } // for(int i = 0;i < n;i++){ // for(int j = 0;j < n;j++){ // cout << dis[i][j] << " "; // } // cout<< endl; // } wt(spfa()); } return 0; }

     

    最新回复(0)