Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1746 Accepted Submission(s): 678
3 1 0.5 0.5 0.5 1 0.4 0.5 0.4 1 3 1 2 2 3 1 3
0.500 0.400 0.500
简单的最短路,不过这道题是乘法运算,所以初始化和判断稍微改变一下就行了,spfa搞定。
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1596
代码:
#include <iostream> #include <stdio.h> #include <memory.h> #include <queue> using namespace std; const int N = 1005; double map[N][N], dist[N]; bool visit[N]; int n, t; void input() { int i, j; for(i = 1; i <= n; i++) for(j = 1; j <= n; j++) scanf("%lf", &map[i][j]); } void spfa(int begin, int end) { int i, now; memset(visit, false, sizeof(visit)); for(i = 0; i <= n; i++) dist[i] = 0; //dist初始化为0 dist[begin] = 1; //dist起点为1 queue<int> Q; Q.push(begin); visit[begin] = true; while(!Q.empty()) { now = Q.front(); Q.pop(); visit[now] = false; for(i = 1; i <= n; i++) { if(dist[i] < dist[now] * map[now][i]) //乘法 { dist[i] = dist[now] * map[now][i]; if(visit[i] == false) { Q.push(i); visit[i] = true; } } } } } void output() { int ti, tj; scanf("%d", &t); while(t--) { scanf("%d %d", &ti, &tj); spfa(ti, tj); if(dist[tj] != 0) printf("%.3lf\n", dist[tj]); else printf("What a pity!\n"); } } int main() { while(scanf("%d", &n) != EOF) { input(); output(); } return 0; }