Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

状压DP(代码)(输出要用%.3f)

Posted by FaustAbsinth at 2019-08-18 14:53:16 on Problem 2686
#include <iostream>
#include <cstdio>
#include <cstring>
#define N 9
#define M 31
using namespace std;
const double INF = 0x3f3f3f3f;
int n, m, p, a, b;
double dp[1 << N][M]; //dp[s][j] 达到j点以及剩余车票为s的状态的最小费用

int t[N];
inline double min(double x, double y) {
	return x > y ? y : x;
}
int main () {
	int u, v, cost;
	double ans;
	while(1) {
		scanf("%d %d %d %d %d", &n, &m, &p, &a, &b);
		if(n == 0 and m == 0 and p == 0 and a == 0 and b == 0) break;
		int d[M][M] = {0};
		ans = INF;
		for(int i = 0; i < n; i++) scanf("%d", &t[i]);
		for(int i = 0; i < p; i++) {
			scanf("%d %d %d", &u, &v, &cost);
			d[u][v] = d[v][u] = cost;
		}
		for(int i = 0; i < 1 << N; i++)
			for(int j = 0; j < M; j++)
				dp[i][j] = INF;
		dp[0][a] = 0; //起点
		for(int s = 0; s < 1 << n; s++) {
			for(int q = 0; q < n; q++) {//枚举剩下车票 
				if(!(s >> q & 1))  //第q张车票没有用过
					for(int i = 1; i <= m; i++)  //从i转移到j,枚举起点 
						for(int j = 1; j <= m; j++)  //枚举终点
							if(d[i][j]) 
								dp[s|(1 << q)][j] = min(dp[s|(1 << q)][j], dp[s][i] + (double)d[i][j] / t[q]);
			}
			ans = min(ans, dp[s][b]); //更新每个状态到达中的的费用
		}
		if(ans == INF) printf("Impossible\n");
		else printf("%.3f\n", ans);
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator