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. [Effective JavaScript 笔记] 第7条:视字符串为16位的代码单元序列

    Unicode编码,基础:它为世界上所有的文字系统的每个字符单位分配一个唯一的整数,该整数介于0~1114111之间,在Unicode术语中称为代码点(code point). 和其它字符编码几乎没有 ...

  2. 专注docker安全:Security Scanning

    导读 Docker毫无疑问是近期运维同学们的热点话题,Docker安全也由此倍受重视,Docker Security Scanning 是一款Docker镜像扫描的安全工具,目前已经在Docker C ...

  3. php页面打开响应时间

    $start_time = array_sum(explode(' ',microtime())); //your code here   $end_time = array_sum(explode( ...

  4. MongoDB概述&语法

    Nosql DB 这是一个非关系型数据库. 通常我们的数据库有三类:  关系型数据库(RDBMS),联机分析处理数据库(OLAP),和菲关系型数据库(NoSql). MongoDB属于第三种,而且是一 ...

  5. Linux下 ntp 时间同步服务ntpd 出现 the NTP socket is in use, exiting 解决

    [root@EPDDB log]# [root@EPDDB log]# ntpdate 10.154.8.200 6 Sep 09:35:09 ntpdate[30210]: the NTP sock ...

  6. LLVM,Clang

    在使用xcode时常常会遇到这2个概念,今天总结一下. wiki中关于llvm的描述: LLVM提供了完整編譯系統的中間層,它會將中間語言(IF, Intermediate form)從編譯器取出與最 ...

  7. PHP+七牛云存储上传图片代码片段

    2014年11月14日 16:37:51 第一段代码是上传单个图片的,第二个是上传多个图片的 //上传到七牛 //单个文件 //formname: 表单名字; pre: 图片Url中显示的图片名字(也 ...

  8. php中正则表达式的匹配和数据验证总结

    正则表达式能匹配复杂的字符串形式,比字符串处理函数功能更加多,只不过执行效率有所降低,但是可以实现非常复杂的匹配,下面总结一下 1.简单的字符串匹配,判断指定字符串是不是在另一个字符串中,和字符串查找 ...

  9. Java for LeetCode 054 Spiral Matrix

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  10. hp unix

    HP-UNIX 命令大全 #vgdisplay -v vgname #lvdisplay -v lvfullpath #pvdisplay -v pvfullpath # ioscan –fnkC d ...