Victor and World

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

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 thevi-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
 
题目大意:给你t组数据,每组数据给出n和m,分别表示n个城市,m条航线,然后会有m行u,v,w分别表示航线的起点和终点以及飞跃这段航线需要的燃油量,问你从城市1至少经过其余所有城市1次最后飞回城市1的的燃油量最少是多少。
 
解题思路:首先去重边,然后跑floyd算出所有城市之间的最短路径dis,注意d[i][i]=0。由于n个数很小,可以看出是道状态压缩题目。我们用dp[s][i]表示当前的城市访问状态是s,最后到达的城市是i的最少燃油耗费。s表示状态,即用二进制表示城市是否到过。状态转移方程为dp[s|(1<<(i-1))][i]=min(dp[s|(1<<(i-1))][i],dp[s][j]+dis[j][i]) i和j应该满足s&(1<<(i-1))==0&& s&(1<<(j-1))!=0。表示s状态还没到过i城市,s状态已经到过j城市。 最后枚举所有终点为1的情况,得到最小值。
 
 
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int d[20][20],dp[(1<<20)][20];
int main(){
int t,n,m,a,b,c;
scanf("%d",&t);
while(t--){
memset(d,INF,sizeof(d));
memset(dp,INF,sizeof(dp));
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
if(d[a][b]>c){
d[a][b]=d[b][a]=c;
}
}
for(int i=1;i<=n;i++)
d[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(d[i][k]<INF&&d[k][j]<INF)
d[i][j] = d[i][j]<(d[i][k]+d[k][j])?d[i][j]:(d[i][k]+d[k][j]);
}
}
}
dp[1][1]=0;
for(int s=1;s<(1<<n);s++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(( (s&(1<<(i-1)))==0 ) &&( (s&(1<<(j-1))) ))
dp[s|(1<<(i-1))][i]=min(dp[s|(1<<(i-1))][i],dp[s][j]+d[j][i]);
}
}
}
int ans=INF;
for(int i=1;i<=n;i++){
ans=min(ans,dp[(1<<n)-1][i]+d[i][1]);
}
printf("%d\n",ans);
}
return 0;
}

  

最新文章

  1. CANopen学习——PDO
  2. Power服务器中KVM克隆新虚拟机
  3. 【转】logback logback.xml常用配置详解(一)&lt;configuration&gt; and &lt;logger&gt;
  4. WebService 用法
  5. phpcms 的实用相关接口,函数,调用方法
  6. linux exec函数家族
  7. asp.net采用OLEDB方式导入Excel数据时提示:未在本地计算机上注册&quot;Microsoft.Jet.OLEDB.4.0&quot; 提供程序&quot;
  8. The Lost Art of C Structure Packing
  9. 新鲜博客出炉www.pubwin2009.net
  10. html5 note
  11. Google免费的SVN服务器管理VS2010代码
  12. php面试题汇总二(基础篇附答案)
  13. C/C++程序在main之前执行代码
  14. animation-timing-function的steps详解
  15. maven安装与基本配置
  16. 【Linux】comm指令
  17. 【SpringBoot】常用注解
  18. 网络之TCP握手总结
  19. kali2.0 使用SSH进行远程登录
  20. BZOJ 1305 dance跳舞(最大流+二分答案)

热门文章

  1. Algorithms - Quick Sort
  2. C#使用Newtonsoft.Json操作json
  3. 【C# MVC】
  4. C#检测系统是否激活[转自StackOverFlow]
  5. 理解CNN中的通道 channel
  6. Swift3.0 UICollectionView简单使用
  7. springboot junit单元测试报错
  8. P4768 [NOI2018]归程
  9. java 柱状图、折线图、饼状图
  10. NowCoder数列(矩阵快速幂变式)