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
dijkstra:
#include<cstdio> #include<cstring> #include<cmath> #include<queue> #define Wi(a) while(a--) #define Si(a) scanf("%d", &a) #define Sch(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define mem(a, b) memset(a, (b), sizeof(a)) #define INF 0x3f3f3f3f #include<algorithm> using namespace std; const int mx = 1010; int n; double map[mx][mx]; double dis[mx]; bool vis[mx]; void init() { for(int i = 1; i <= n; i++ ){ for(int j = 1; j <= n; j++ ) map[i][j] = 0; } } void dijkstra(int s, int t) { mem(vis, 0); int i, j, k; for(i = 1; i <= n; i++) dis[i] = map[s][i]; vis[s] = 1; for(i = 1; i < n; i++) { double minn = 0; k = s; for(j = 1; j <= n; j++) { if(!vis[j] && minn < dis[j]) { minn = dis[j]; k = j; } } vis[k] = 1; for(j = 1; j <= n; j++) { if(!vis[j]) dis[j] = max(dis[j], dis[k]*map[k][j]);//是乘不是加。。。 } } if(dis[t] != 0) printf("%.3lf\n", dis[t]); else puts("What a pity!"); } int main() { while(Si(n)==1) { init(); int i,j,k; for(i = 1; i <= n; i++) { for(j = 1; j <= n; j++) { scanf("%lf", &map[i][j]); } } int m; Si(m); Wi(m) { int a, b; scanf("%d%d", &a, &b); if(a == b) printf("1.000\n"); else dijkstra(a, b); } } return 0; }
#include<cstdio> #include<cstring> #include<cmath> #include<queue> #define Wi(a) while(a--) #define Si(a) scanf("%d", &a) #define Sch(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define mem(a, b) memset(a, (b), sizeof(a)) #define INF 0x3f3f3f3f #include<algorithm> using namespace std; const int mx = 1010; int n; double map[mx][mx]; void floyd() { int i, j ,k; for(k = 1; k <= n; k++){ for(i = 1; i <= n; i++){ for(j = 1; j <= n; j++) if(map[i][j] < map[i][k]*map[k][j]) map[i][j] = map[i][k]*map[k][j]; } } } int main() { while(Si(n)==1) { mem(map, 0); int i,j,k; double c; for(i = 1; i <= n; i++) { for(j = 1; j <= n; j++) { scanf("%lf", &c); map[i][j] = c; } } floyd(); int m; Si(m); Wi(m) { int s, t; scanf("%d%d", &s, &t); if(s == t) printf("1.000\n"); else{ if(map[s][t] != 0) printf("%.3lf\n", map[s][t]); else puts("What a pity!"); } } } return 0; }
#include<cstdio> #include<cstring> #include<cmath> #include<queue> #define mem(a, b) memset(a, (b), sizeof(a)) #define Wi(a) while(a--) #define Si(a) scanf("%d", &a) #define Pi(a) printf("%d\n", (a)) #define INF 0x3f3f3f #include<algorithm> using namespace std; const int mx = 1010; const int mr = 1000*1000; double dis[mx]; bool vis[mx]; int head[mr], edgenum, n; struct node{ int from, to; double val; int next; }; node edge[mr]; void init() { edgenum = 0; mem(head, -1); } void add(int a, int b, double c) { node E = { a, b, c, head[a] }; edge[edgenum] = E; head[a] = edgenum++; } void spfa(int s, int t) { queue<int> q; mem(vis, 0); mem(dis, 0); vis[s] = 1; dis[s] = 1; q.push(s); while(!q.empty()){ int u = q.front(); q.pop(); vis[u] = 0; for(int i = head[u]; i != -1; i = edge[i].next){ int v = edge[i].to; if(dis[v] < dis[u]*edge[i].val){ dis[v] = dis[u]*edge[i].val; if(!vis[v]){ vis[v] = 1; q.push(v); } } } } if(dis[t] != 0) printf("%.3lf\n", dis[t]); else puts("What a pity!"); } void getmap() { double c; for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ scanf("%lf", &c); add(i, j, c); //add(j, i, c);//单向道路!!! } } } int main(){ while(Si(n)==1) { init(); getmap(); int m; Si(m); Wi(m){ int a, b; scanf("%d%d", &a, &b); spfa(a, b); } } return 0; }