[状压dp]POJ2686 Traveling by Stagecoach

题意: m个城市, n张车票, 每张车票$t_i$匹马, 每张车票可以沿某条道路到相邻城市, 花费是路的长度除以马的数量. 求a到b的最小花费, 不能到达输出Impossible

$1\le n\le8$

$2\le m\le30$

 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cctype>
#include <cmath>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
typedef long long LL;
typedef long double LD;
#define INFF 0x3f3f3f3f
#define INF 2139062143
#define pi acos(-1.0)
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
typedef pair<int, int> PI;
typedef pair<int, PI> PP;
#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//LL quick(LL a, LL b){LL ans=1;while(b){if(b & 1)ans*=a;a=a*a;b>>=1;}return ans;}
//inline int read(){char ch=' ';int ans=0;while(ch<'0' || ch>'9')ch=getchar();while(ch<='9' && ch>='0'){ans=ans*10+ch-'0';ch=getchar();}return ans;}
//inline void print(LL x){printf(LLD, x);puts("");}
//inline void read(int &x){char c = getchar();while(c < '0') c = getchar();x = c - '0'; c = getchar();while(c >= '0'){x = x * 10 + (c - '0'); c = getchar();}} int t[];
int mp[][];
double dp[<<][]; // 剩下的车票状态 现在在v的最小花费
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int n, m, p, a, b;
while(~scanf("%d%d%d%d%d", &n, &m, &p, &a, &b) && (n || m || p || a || b))
{
for(int i=;i<n;i++)
scanf("%d", &t[i]);
memset(mp, -, sizeof(mp));
while(p--)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
u--, v--;
if(mp[u][v]<)
mp[u][v]=mp[v][u]=w;
else
mp[u][v]=mp[v][u]=min(mp[u][v], w);
}
memset(dp, , sizeof(dp));
dp[(<<n)-][a-]=;
double ans=INF;
for(int s=(<<n)-;s>=;s--)
{
ans=min(ans, dp[s][b-]);
for(int v=;v<m;v++)
for(int i=;i<n;i++)
if(s>>i & )
for(int u=;u<m;u++)
if(mp[v][u]>=)
dp[s & ~(<<i)][u]=min(dp[s & ~(<<i)][u], dp[s][v]+mp[v][u]*1.0/t[i]);
// 使用车票i , v->u
}
if(ans==INF)
printf("Impossible\n");
else
printf("%.3lf\n", ans);
}
return ;
}
上一篇:富文本vue-quill-editor修改图片上传方法


下一篇:vue-quill-editor + iview 实现富文本编辑器及图片上传