Travelling
Travelling
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2554 Accepted Submission(s): 746
Problem Description
After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best choice!He has decided to visit n cities(he insists on seeing all the cities!And he does not mind which city being his start station because superman can bring him to any city at first but only once.), and of course there are m roads here,following a fee as usual.But Mr Acmer gets bored so easily that he doesn't want to visit a city more than twice!And he is so mean that he wants to minimize the total fee!He is lazy you see.So he turns to you for help.
Input
There are several test cases,the first line is two intergers n(1<=n<=10) and m,which means he needs to visit n cities and there are m roads he can choose,then m lines follow,each line will include three intergers a,b and c(1<=a,b<=n),means there is a road between a and b and the cost is of course c.Input to the End Of File.
Output
Output the minimum fee that he should pay,or -1 if he can't find such a route.
Sample Input
2 1
1 2 100
3 2
1 2 40
2 3 50
3 3
1 2 3
1 3 4
2 3 10
Sample Output
100
90
7
Source
2009 Multi-University Training Contest 11 - Host by HRBEU
Recommend
gaojie
把所有的城市访问次数合在一起作为状态的描述,因为每个城市最多访问2次,所以状态压缩的结果是3进制的.
状态转移方程:DP[i][j]=Min(DP[i.pre][k]+G[k][j])
DP[i][j]表示达到i状态并且此时位于j城市所需要的最小花费.
#include<stdio.h>
#include<string.h>
int N,M;
int state[][];
int DP[][];
int len[];
int G[][];
int s[];
int f(int x)
{
int i;
for (i=;i<;i++)
if (state[x][i]==) return i;
return ;
}
void init()
{
int i,j;
s[]=;
for (i=;i<;i++) s[i]=s[i-]*;
for (i=;i<s[];i++)
{
int tmp=i;
for (j=;j<;j++)
{
state[i][j]=tmp%;
tmp/=;
}
}
for (i=;i<s[];i++) len[i]=f(i);
}
int main()
{
init();
while (scanf("%d%d",&N,&M)!=EOF)
{
int i,j,k;
memset(G,0x3f,sizeof(G));
for (i=;i<=M;i++)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
u--;
v--;
if (c<G[u][v]) G[u][v]=c;
if (c<G[v][u]) G[v][u]=c;
}
memset(DP,0x3f,sizeof(DP));
for (i=;i<N;i++) DP[s[i]][i]=;
for (i=;i<s[N];i++)
for (j=;j<N;j++)
if (state[i][j])
{
int last=i-s[j];
for (k=;k<N;k++)
if (DP[last][k]+G[k][j]<DP[i][j] && state[last][k])
DP[i][j]=DP[last][k]+G[k][j];
}
int Min=;
for (i=;i<s[N];i++)
if (len[i]==N)
for (j=;j<N;j++)
if (state[i][j] && DP[i][j]<Min)
Min=DP[i][j];
if (Min==) printf("-1\n");
else printf("%d\n",Min);
}
return ;
}
Travelling的更多相关文章
- ACM: 限时训练题解- Travelling Salesman-最小生成树
Travelling Salesman After leaving Yemen, Bahosain now works as a salesman in Jordan. He spends mos ...
- Codeforce - Travelling Salesman
After leaving Yemen, Bahosain now works as a salesman in Jordan. He spends most of his time travelli ...
- URAL 1077 Travelling Tours(统计无向图中环的数目)
Travelling Tours Time limit: 1.0 secondMemory limit: 64 MB There are N cities numbered from 1 to N ( ...
- HDU-3001 Travelling
http://acm.hdu.edu.cn/showproblem.php?pid=3001 从任何一个点出发,去到达所有的点,但每个点只能到达2次,使用的经费最小.三进制 Travelling Ti ...
- Bzoj 1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛 动态规划
1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1006 Solved: ...
- BZOJ1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛
1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 762 Solved: ...
- BZOJ 1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛( dp )
一道水 dp ...然后我一开始用 BFS ...结果 MLE 了... dp[ i ][ j ][ k ] 由它四个方向上的 k - 1 转移. -------------------------- ...
- hdu 3001 Travelling (TSP问题 )
Travelling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 3001 Travelling(状态压缩 三进制)
Travelling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- Windows/Centos安装GO语言环境
转载:http://www.haiyun.me/archives/1009.html Centos下使用epel源安装: 1 yum install golang Centos/Linux下源码安装g ...
- BestCoder Round #60 1002
Problem Description You are given two numbers NNN and MMM. Every step you can get a new NNN in the w ...
- Linux Tomcat 简介
如今,基于Web的应用越来越多,传统的Html已经满足不了如今的需求.我们需要一个交互式的Web,于是便诞生了各种Web语言.如Asp,Jsp,Php等.当然,这些语言与传统的语言有着密切的联系,如P ...
- HDOJ 1870
#include<stdio.h> #include<stack> #include<string.h> #include<iostream> usin ...
- 【转】android中Uri.parse()用法
1,调web浏览器 Uri myBlogUri = Uri.parse("http://xxxxx.com"); returnIt = new Intent(Intent.ACTI ...
- nc 常用命令
nc(NetCat),在网络工具中有”瑞士军刀”的美誉,它短小精悍,功能强大,下面分享一些我平时经常用到的功能,更多的功能请google之. 1.基本参数想要连接到某处: nc [-options] ...
- 【OpenStack】OpenStack系列11之namaspace&openvswitch原理实践
Namespace实现网络隔离与互通 新建ns: ip netns add foo 查看ns: ip netns 查看ns详细配置: ip netns exec foo ip addr 设置ns内部l ...
- 【SpringMVC】SpringMVC系列11之Restful的CRUD
11.Restful的CRUD 11.1.需求 11.2.POST转化为PUT.DELETE的fileter 11.3.查询所有 11.4.添加 11.5.删除 优雅的 REST 风格的资 ...
- Bitwise AND of Numbers Range
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- kettle转换JavaScript加载外部js文件
日常开发中,时常会出现这样一种情况.有大量的函数是通用的.而每个JavaScript里面写一遍,给维护带来很大的困扰.因而需要将公共的函数写在外部js文件中.这时就需要引入外部的公共文件了.下面是在转 ...