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的更多相关文章

  1. ACM: 限时训练题解- Travelling Salesman-最小生成树

    Travelling Salesman   After leaving Yemen, Bahosain now works as a salesman in Jordan. He spends mos ...

  2. Codeforce - Travelling Salesman

    After leaving Yemen, Bahosain now works as a salesman in Jordan. He spends most of his time travelli ...

  3. URAL 1077 Travelling Tours(统计无向图中环的数目)

    Travelling Tours Time limit: 1.0 secondMemory limit: 64 MB There are N cities numbered from 1 to N ( ...

  4. HDU-3001 Travelling

    http://acm.hdu.edu.cn/showproblem.php?pid=3001 从任何一个点出发,去到达所有的点,但每个点只能到达2次,使用的经费最小.三进制 Travelling Ti ...

  5. Bzoj 1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛 动态规划

    1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1006  Solved: ...

  6. BZOJ1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛

    1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 762  Solved:  ...

  7. BZOJ 1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛( dp )

    一道水 dp ...然后我一开始用 BFS ...结果 MLE 了... dp[ i ][ j ][ k ] 由它四个方向上的 k - 1 转移. -------------------------- ...

  8. hdu 3001 Travelling (TSP问题 )

    Travelling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. hdu 3001 Travelling(状态压缩 三进制)

    Travelling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. HNU 12826 Balloons Colors

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12826&courseid=268 #include&l ...

  2. SGU 180 Inversions(离散化 + 线段树求逆序对)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...

  3. Win10如何隐藏Windows Defender任务栏图标

    导读 Windows 10 至发布以来就内置集成了 Windows Defender 安全防护应用,但有许多用户平常压根儿就没注意到它的存在.微软为了使安全防护功能更加明显,Windows 10 周年 ...

  4. js 中数组或者对象的深拷贝和浅拷贝

    浅拷贝 : 就是两个js 对象指向同一块内存地址,所以当obj1 ,obj2指向obj3的时候,一旦其中一个改变,其他的便会改变! 深拷贝:就是重新复制一块内存,这样就不会互相影响. 有些时候我们定义 ...

  5. Linux 磁盘的组成

    基本结构 磁道,扇区,柱面和磁头数 硬盘最基本的组成部分是由坚硬金属材料制成的涂以磁性介质的盘片,不同容量硬盘的盘片数不等.每个盘片有两面,都可记录信息. 每个磁道被分成许多扇形的区域,每个区域叫一个 ...

  6. linux 下查看某个端口是否被占用

    lsof -i:端口号 转自: http://my.oschina.net/u/193184/blog/146885

  7. kettle转换JavaScript加载外部js文件

    日常开发中,时常会出现这样一种情况.有大量的函数是通用的.而每个JavaScript里面写一遍,给维护带来很大的困扰.因而需要将公共的函数写在外部js文件中.这时就需要引入外部的公共文件了.下面是在转 ...

  8. 用cocos2dx实现模态对话框

    ui部分使用了cocoStudio,注意这里没有实现怎么屏蔽其他的输入事件,其他的文档已经太多了,我这里使用的cocoStudio的控件自己的特性. 这里强烈推荐一下cocoStudio,虽然现在还有 ...

  9. HDU1004 查气球

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  10. google maps js v3 api教程(2) -- 在地图上添加标记

    原文链接 google maps javascript官方文档:https://developers.google.com/maps/documentation/javascript/ 我们在创建地图 ...