HDU - 5418:Victor and World

Victor and World

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


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.

1T20 .

1n16 .

1m100000 .

1wi100 .

1ui,vin .
 

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

思路:因为每个城市可以访问多次(至少一次),用Floyd算法求出任意两点间最短路。然后再用状压DP。

#include<bits/stdc++.h>
using namespace std;
long long d[1<<16][16],w[16][16];
int main()
{
	int T,n,m;cin>>T;
	while(T--)
	{
		cin>>n>>m;
		memset(d,0x3f3f3f3f,sizeof d);
		memset(w,0x3f3f3f3f,sizeof w);
		for(int i=0;i<n;i++)w[i][i]=0;
		while(m--)
		{
			int x,y;
			long long v;
			cin>>x>>y>>v;
			w[x-1][y-1]=w[y-1][x-1]=min(w[x-1][y-1],v);
		}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				for(int k=0;k<n;k++)w[i][j]=min(w[i][j],w[i][k]+w[k][j]);
			}
		}
		d[1][0]=0;
		for(int i=1;i<(1<<n);i++)
		{
			for(int j=0;j<n;j++)
			{
				if((i&(1<<j))==0)continue;
				for(int k=0;k<n;k++)
				{
					if(i&(1<<k))continue;
					d[i|(1<<k)][k]=min(d[i|(1<<k)][k],d[i][j]+w[j][k]);
				}
			}
		}
		long long ans=1e9+7;
		for(int i=0;i<n;i++)ans=min(ans,d[(1<<n)-1][i]+w[i][0]);
		cout<<ans<<endl;
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值