Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
状态压缩,附代码一份#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; int n,m,p,a,b; int tickets[20]; const double INF = 1e9; struct edge{ int u,v,cost,next; }Edge[1000]; double dp[1<<10][50];//dp[S][v]到位置v且还剩S的票的最小值 int cnt; int head[1000]; void add_edge(int u,int v,int cost) { Edge[cnt].u = u; Edge[cnt].v = v; Edge[cnt].cost = cost; Edge[cnt].next = head[u]; head[u] = cnt++; } double solve(int s,int g) { double ans = INF; for(int S = 0;S < 1<<n;S++) fill(dp[S],dp[S]+50,INF); dp[(1<<n)-1][s] = 0; for(int S = (1<<n)-1;S >= 0;--S) { for(int u = 1;u <= m;u++) { for(int t = 0;t < n;t++) { for(int e = head[u];e!=-1;e = Edge[e].next) { int v = Edge[e].v; int cost = Edge[e].cost; if((S>>t)&1) { dp[S&~(1<<t)][v] = min(dp[S&~(1<<t)][v],dp[S][u] + (double)cost/(double)tickets[t]); if(v == g) { ans = min(ans,dp[S&~(1<<t)][v]); } } } } } } return ans; } int main() { while(~scanf("%d%d%d%d%d",&n,&m,&p,&a,&b) && n+m+p+a+b) { memset(head,-1,sizeof(head)); cnt = 0; for(int i = 0;i < n;i++) scanf("%d",&tickets[i]); for(int i = 0;i < p;i++) { int u,v,c; scanf("%d%d%d",&u,&v,&c); add_edge(u,v,c); add_edge(v,u,c); } double ans = solve(a,b); if(ans == INF) puts("Impossible"); else printf("%.3f\n",ans); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator