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. MFC 最大化 的时候控件 按比例变大

    在dlg类头文件中声明CPoint Old; 在BEGIN_MESSAGE_MAP()和END_MESSAGE_MAP()声明一个映射:ON_WM_SIZE() 这样以后就可以在M_SIZE事件的时候 ...

  2. 使用Cydia Substrate 从Native Hook Android Java世界

    这里介绍了如何使用Cydia Substrate Hook安卓Java世界.这篇文章介绍如何从Native中Hook 安卓Java世界. 手机端配置见之前文章. 一.建立工程 建立一个Android工 ...

  3. HDOJ 2544

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  4. javascript memoization递归优化

    memoize优化递归 function createRec(callback, cache) { cache = cache || []; var rec = function(n) { (n in ...

  5. 代码风格与树形DP

    Streaming很惨,不过因为比赛之间没有提交过就没掉(或掉了)rating.第二题是一个树形DP,但是我都在想第一题了,简直作死. 看着神犇的代码我也是醉了...各种宏,真是好好写会死系列. 看到 ...

  6. Metasploit是一款开源的安全漏洞检测工具,

    Metasploit是一款开源的安全漏洞检测工具,可以帮助安全和IT专业人士识别安全性问题,验证漏洞的缓解措施,并管理专家驱动的安全性进行评估,适合于需要核实漏洞的安全专家,同时也适合于强大进攻能力的 ...

  7. systemd在各个linux发行版的普及

    后面我要说下自己的意见: 原则如果阻碍了进步,那还算个屁,不客气地说,UNIX 原则已经过时了. 移植性问题:我除了 Mac 外不用任何 BSD 系统,当然 Mac 上一般只做开发不做运维(但就算如此 ...

  8. 42.旋转数组的最小元素[Get min value of rotated array]

    [题目] 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个排好序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3, 4, 5, 1, 2}为{1, 2, 3, 4, 5 ...

  9. 快速排序模板qsort(转载)

     qsort  用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *)); 各 ...

  10. August 6th, 2016, Week 32nd, Saturday

    It is not the mountain we conquer, but ourselvess. 我们征服的不是高山,而是我们自己. Difficulties and obstacles, jus ...