首页 > 代码库 > HDU 3001 Travelling
HDU 3001 Travelling
Travelling
Time Limit: 3000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 300164-bit integer IO format: %I64d Java class name: Main
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 11 2 1003 21 2 402 3 503 31 2 31 3 42 3 10
Sample Output
100907
Source
2009 Multi-University Training Contest 11 - Host by HRBEU
解题:状压dp...
s[i][j]表示在状态i下,city j被访问了 多少次。
只有s[i][j]中的所有j都大于0 即至少访问过一次,才能把此态视为终态。否则,不能视为终态。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 int tri[] = {0,1,3,9,27,81,243,729,2187,6561,19683,59049};18 int e[12][12],dp[59049][12],s[59049][12] = {0};19 int main() {20 int n,m,x,y,z;21 for(int i = 0; i < 59049; i++) {22 int tmp = i;23 for(int k = 1; k <= 10; k++) {24 s[i][k] = tmp%3;25 tmp /= 3;26 }27 }28 while(~scanf("%d %d",&n,&m)) {29 for(int i = 1; i <= n; i++)30 for(int j = 1; j <= n; j++) e[i][j] = INF;31 for(int i = 0; i < m; i++) {32 scanf("%d %d %d",&x,&y,&z);33 if(e[x][y] > z) e[x][y] = e[y][x] = z;34 }35 for(int i = 0; i < tri[n+1]; i++)36 for(int j = 1; j <= 10; j++) dp[i][j] = INF;37 for(int i = 0; i <= 10; i++) dp[tri[i]][i] = 0;38 int ans = INF;39 for(int i = 0; i < tri[n+1]; i++) {40 bool flag = true;41 for(int j = 1; j <= n; j++) {42 if(s[i][j] == 0) flag = false;43 for(int k = 1; k <= n; k++){44 if(j == k || e[j][k] == INF || s[i][k] == 2) continue;45 int ns = i + tri[k];46 dp[ns][k] = min(dp[ns][k],dp[i][j] + e[j][k]);47 }48 }49 for(int k = 1; flag && k <= n; k++)50 ans = min(ans,dp[i][k]);51 }52 ans == INF?puts("-1"):printf("%d\n",ans);53 }54 return 0;55 }
HDU 3001 Travelling
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。