Traveling by Stagecoach 状态压缩裸题

                              Traveling by Stagecoach

dp[s][v]  从源点到达  v,状态为s,v的最小值。  for循环枚举就行了。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <string>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <stack>
11 #include <queue>
12 #include <sstream>
13 #include <iomanip>
14 using namespace std;
15 typedef long long LL;
16 const double INF=0x4fffffff;
17 const double EXP=1e-5;
18 const int MS=10;
19 const int SIZE=31;
20
21 double dp[1<<MS][SIZE];
22
23 int edges[SIZE][SIZE];
24 int cnt[MS];
25
26 int start,final,tickets,citys,edge;
27
28 void solve()
29 {
30       for(int i=0;i<(1<<tickets);i++)
31             fill(dp[i],dp[i]+SIZE,INF);
32       dp[(1<<tickets)-1][start-1]=0;
33       double res=INF;
34       for(int i=(1<<tickets)-1;i>=0;i--)
35       {
36             res=min(res,dp[i][final-1]);
37             for(int u=0;u<citys;u++)
38             {
39                   for(int j=0;j<tickets;j++)
40                   {
41                         if((i>>j)&1)
42                         {
43                               for(int v=0;v<citys;v++)
44                                     if(edges[u][v]>=0)
45                                     dp[i-(1<<j)][v]=min(dp[i-(1<<j)][v],dp[i][u]+double(edges[u][v])/cnt[j]);
46                         }
47                   }
48             }
49       }
50       if(res+EXP>=INF)
51             printf("Impossible\n");
52       else
53             printf("%.3lf\n",res);
54 }
55
56 int main()
57 {
58       while(scanf("%d%d%d%d%d",&tickets,&citys,&edge,&start,&final)==5&&tickets)
59       {
60
61             for(int i=0;i<tickets;i++)
62             {
63                   scanf("%d",&cnt[i]);
64             }
65             memset(edges,-1,sizeof(edges));
66             for(int i=0;i<edge;i++)
67             {
68                   int x,y,z;
69                   scanf("%d%d%d",&x,&y,&z);
70                   edges[x-1][y-1]=edges[y-1][x-1]=z;
71             }
72             solve();
73       }
74       return 0;
75 }
时间: 2024-08-28 14:55:56

Traveling by Stagecoach 状态压缩裸题的相关文章

北大ACM2686——Traveling by Stagecoach~~状态压缩DP

最近才看书,看到状态压缩.对于状态压缩DP,其实就是集合上的DP. 这需要我们了解一些位运算: 集合{0,1,2,3,....,n-1}的子集可以用下面的方法编码成整数 像这样,一些集合运算就可以用如下的方法来操作: 1.空集....................0 2.只含有第i个元素的集合{i}................1 << i 3.含有全部n个元素的集合{0,1,2,3,....,n - 1}.............(1 << n) - 1 4.判断第i个元素是

POJ 2686 Traveling by Stagecoach (状态压缩DP)

Traveling by Stagecoach Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2776   Accepted: 996   Special Judge Description Once upon a time, there was a traveler. He plans to travel using stagecoaches (horse wagons). His starting point and

poj2686(Traveling by Stagecoach)状态压缩dp

Description Once upon a time, there was a traveler. He plans to travel using stagecoaches (horse wagons). His starting point and destination are fixed, but he cannot determine his route. Your job in this problem is to write a program which determines

POJ-3254-Corn Fields-DP+状态压缩(入门题)

题目链接:http://poj.org/problem?id=3254 题目意思:给你一个n*m的牧场,叫你带牛去吃草,其中0代表没有草不可以放牧,1代表有草可以放牧.而且两头牛不可以相邻,叫你求所有可能的放牧方案. 思路:这是个状态压缩的基础题,刚学状态压缩的可以用这个题目来理解状态压缩:(如果是刚学DP我建议理解题意后先粗略的看一下代码后再边看代码边看我的思路,效果更佳) 1.题目告诉我们两头牛不能相邻,那么我们就可以枚举出每一行的所有情况,并且用数组st保存起来:怎么枚举每一种情况呢,题目

poj1185 状态压缩经典题

状态压缩的好题,直接求会爆内存,先把所有可能的状态求出来存在stk里,然后f[i][k][t]表示i行状态为t,i-1状态为k,由i-1状态来推出i状态即可 注意要打好边际条件的状态,并且某个可行状态必须由前一个可行状态推出 /* f[i][k][t]表示第i行状态为t,第i-1行状态为k的炮兵数 边际条件:第一行为任意可行状态即dp[1][1][i]=num[i] */ #include<bits/stdc++.h> using namespace std; #define ll long

dp之状态压缩水题总结

状态压缩dp 其实就是 将状态表示成二进制,然后用位运算进行求解 poj 3311 Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5080   Accepted: 2706 Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unf

状态压缩简单题

状态压缩·一 题目传送:#1044 : 状态压缩·一 AC代码: #include <map> #include <set> #include <list> #include <cmath> #include <deque> #include <queue> #include <stack> #include <bitset> #include <cctype> #include <cstdi

hdu4064 三进制状态压缩 好题!

还不太会做这类题,总之感觉有点难啊. 用深搜代替打表求出一行所有的可行状态,注意要进行剪枝 这是自己理解的代码,但是tle了 #include<bits/stdc++.h> using namespace std; #define mod 1000000007 #define maxn 600005 int n,m,dp[2][maxn],sum; char mp[15][15][15]; int a[200],tag,flag,s[15]; // void dfs(int r,int num

[HDU]5094Maze(状态压缩BFS)

状态压缩的题,第一次WA了,怎么改都不对,交了十几遍,之后重新写了一个,1A了 #include<cstdio> #include<queue> #include<cstring> #include<algorithm> using namespace std; const int maxn = 51; const int dir[4][2] = {{0,1},{-1,0},{0,-1},{1,0}}; int n,m,t; int door[maxn][m