hdoj 1596 find the safest road
阿新 • • 發佈:2017-06-07
mission 能夠 trac tdi sub -a 代碼 tracking spf
Total Submission(s): 9168 Accepted Submission(s): 3238
Problem Description XX星球有非常多城市,每一個城市之間有一條或多條飛行通道。可是並非全部的路都是非常安全的,每一條路有一個安全系數s,s是在 0 和 1 間的實數(包含0,1),一條從u 到 v 的通道P 的安全度為Safe(P) = s(e1)*s(e2)…*s(ek) e1,e2,ek是P 上的邊 ,如今8600 想出去旅遊。面對這這麽多的路,他想找一條最安全的路。
Input 輸入包含多個測試實例,每一個實例包含:
第一行:n。n表示城市的個數n<=1000;
接著是一個n*n的矩陣表示兩個城市之間的安全系數,(0能夠理解為那兩個城市之間沒有直接的通道)
接著是Q個8600要旅遊的路線,每行有兩個數字,表示8600所在的城市和要去的城市
Output 假設86無法達到他的目的地,輸出"What a pity!",
其它的輸出這兩個城市之間的最安全道路的安全系數,保留三位小數。
Sample Input
Sample Output
find the safest road
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9168 Accepted Submission(s): 3238
Problem Description XX星球有非常多城市,每一個城市之間有一條或多條飛行通道。可是並非全部的路都是非常安全的,每一條路有一個安全系數s,s是在 0 和 1 間的實數(包含0,1),一條從u 到 v 的通道P 的安全度為Safe(P) = s(e1)*s(e2)…*s(ek) e1,e2,ek是P 上的邊 ,如今8600 想出去旅遊。面對這這麽多的路,他想找一條最安全的路。
可是8600 的數學不好,想請你幫忙 ^_^
Input 輸入包含多個測試實例,每一個實例包含:
第一行:n。n表示城市的個數n<=1000;
接著是一個n*n的矩陣表示兩個城市之間的安全系數,(0能夠理解為那兩個城市之間沒有直接的通道)
接著是Q個8600要旅遊的路線,每行有兩個數字,表示8600所在的城市和要去的城市
Output 假設86無法達到他的目的地,輸出"What a pity!",
其它的輸出這兩個城市之間的最安全道路的安全系數,保留三位小數。
Sample Input
3 1 0.5 0.5 0.5 1 0.4 0.5 0.4 1 3 1 2 2 3 1 3
Sample Output
0.500 0.400 0.500 代碼1:dijkstra#include<stdio.h> #include<string.h> #include<algorithm> #define INF 0x3f3f3f using namespace std; double map[1010][1010],low[1010]; int n,vis[1010]; void dijkstra(int x) { int i,j,next; double max; memset(vis,0,sizeof(vis)); for(i=1;i<=n;i++) { low[i]=map[x][i]; } vis[x]=1; for(i=1;i<n;i++) { max=0; for(j=1;j<=n;j++) { if(!vis[j]&&max<low[j]) { max=low[j]; next=j; } } vis[next]=1; for(j=1;j<=n;j++) { if(!vis[j]&&low[j]<map[next][j]*low[next]) { low[j]=map[next][j]*low[next]; } } } } int main() { int i,j,b,q,c; double a; while(scanf("%d",&n)!=EOF) { memset(map,0,sizeof(map)); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%lf",&a); map[i][j]=map[j][i]=max(map[i][j],a); } } scanf("%d",&q); for(i=1;i<=q;i++) { scanf("%d%d",&b,&c); dijkstra(b); if(low[c]==0) printf("What a pity!\n"); else printf("%.3lf\n",low[c]); } } return 0; }
代碼2:spfa#include<stdio.h> #include<stdlib.h> #include<queue> #include<algorithm> #include<string.h> #define INF 0x3f3f3f #define MAXN 1000+10 #define MAXM 500000 using namespace std; double low[MAXN]; int n,top,vis[MAXN]; int head[MAXN]; double num[MAXN][MAXN]; struct record { int to,next; double val; }edge[MAXM]; void init() { top=0; for(int i=1;i<=n;i++) { head[i]=-1; vis[i]=0; } } void add(int a,int b,double c) { edge[top].to=b; edge[top].val=c; edge[top].next=head[a]; head[a]=top++; } void spfa(int x) { int i,j; queue<int>q; for(i=1;i<=n;i++) { low[i]=0; } vis[x]=1; low[x]=1; q.push(x); while(!q.empty()) { int u=q.front(); vis[u]=0; q.pop(); for(i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(low[v]<low[u]*edge[i].val) { low[v]=low[u]*edge[i].val; if(!vis[v]) { vis[v]=1; q.push(v); } } } } } int main() { int i,j,s,e,q; while(scanf("%d",&n)!=EOF) { init(); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%lf",&num[i][j]); if(i>j) { add(i,j,num[i][j]); add(j,i,num[j][i]); } } } scanf("%d",&q); for(i=0;i<q;i++) { scanf("%d%d",&s,&e); spfa(s); if(low[e]==0) printf("What a pity!\n"); else printf("%.3lf\n",low[e]); } } return 0; }
hdoj 1596 find the safest road