夏夜、

心若平似镜、何题不AC。

POJ 2686 Traveling by Stagecoach 状态DP

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI  3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
    return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
    return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else

    freopen("d:\\in1.txt","r",stdin);
    freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
    int ch;
    while((ch=getchar())!=EOF) {
        if(ch!=' '&&ch!='\n')return ch;
    }
    return EOF;
}

double dp[33][1<<8];
int t[8];
int g[33][33];
int n,m,p,a,b;
int read()
{
    scanf("%d%d%d%d%d",&n,&m,&p,&a,&b);
    if(n==0)return 0;
    for(int i=0;i<n;i++)
        scanf("%d",&t[i]);
    memset(g,-1,sizeof(g));
    for(int i=1;i<=p;i++)
    {
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        g[u][v]=g[v][u]=w;
    }
    return 1;
}
double dfs(int u,int k,int st)
{
    if(u==b)return 0;
    if(dp[u][st]>=0)return dp[u][st];
    if(k==n)return INF;
    double &ans=dp[u][st];
    ans=INF;
    for(int i=1;i<=m;i++)if(g[u][i]!=-1)
    {
        for(int j=0;j<n;j++)if(!(st&(1<<j)))
        {
            ans=min(ans,(double)g[u][i]/t[j]+dfs(i,k+1,st|(1<<j)));
        }
    }
    return ans;
}
int main()
{
    while(read())
    {
        memset(dp,-1,sizeof(dp));
        double res=dfs(a,0,0);
        if(res>=INF)printf("Impossible\n");
        else printf("%lf\n",res);
    }
    return 0;
}
View Code

 

posted on 2014-03-11 22:36  BMan、  阅读(171)  评论(0编辑  收藏  举报

导航