HDU1596 find the safest road 最短路问题Dijskra+最小堆
2017-08-04 17:22
260 查看
Author
find the safest road
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 13966 Accepted Submission(s): 4923
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
题意:不用翻译了
思路:权值为0~1,乘法取对数变加法,取对数再取相反数后变为最短路问题。算法用Dijskra+最小堆
#include<cstdio> #include<cmath> #include<queue> #define INF 2100000000 using namespace std; double safety[1005][1005],dis[1005]; int N; class cmp { public: bool operator()(int a,int b) { return dis[a]>dis[b]; } }; double dijskra(int start,int goal) { priority_queue<int,vector<int> ,cmp> minHeap; bool vis[1005]={0}; int i,j,now; for(j=0;j<N;j++) dis[j]=INF; dis[start]=0; minHeap.push(start); while(!minHeap.empty()) { now=minHeap.top(); minHeap.pop(); //cout<<"top:"<<now<<' '<<pow(10,-dis[now])<<endl; vis[now]=true; if(now==goal) break; for(i=0;i<N;i++) { if(vis[i]==false&&dis[now]+safety[now][i]<dis[i]) { //cout<<dis[now]<<'+'<<safety[now][i]<<'('<<pow(10,-safety[now][i])<<')'<<'<'<<dis[i]<<endl; dis[i]=dis[now]+safety[now][i]; minHeap.push(i); } } } return pow(10,-dis[goal]); } int main() { int Q,x,y,i,j; double ans; while(scanf("%d",&N)!=EOF) { for(i=0;i<N;i++) { for(j=0;j<N;j++) { scanf("%lf",&safety[i][j]); if(safety[i][j]==0) safety[i][j]=INF; else safety[i][j]=-log10(safety[i][j]); } } scanf("%d",&Q); for(i=0;i<Q;i++) { scanf("%d%d",&x,&y); ans=dijskra(x-1,y-1); if(ans!=pow(10,-INF)) printf("%.3f\n",ans); else printf("What a pity!\n"); } } return 0; } /*自编数据 4 1 0.6 0.5 0.1 0 1 0.4 0.5 0 0.5 1 0.5 0 0 0 1 16 1 1 1 2 1 3 1 4 2 1 2 2 2 3 2 4 3 1 3 2 3 3 3 4 4 1 4 2 4 3 4 4 */
相关文章推荐
- HDU 1596 find the safest road 【floyd】
- hdu 1596 find the safest road(最短路径)
- HDU 1596 find the safest road (最短路)
- hdu 1596 find the safest road(乘积最短路)
- hdu 1596 find the safest road (最短路径)
- 两道最短路裸题(HDU 2544 最短路 / HDU 1596 find the safest road)
- HDU-1596 find the safest road
- hdu 1596 find the safest road (floyd)
- HDU 1596 find the safest road (spfa算法)
- HDU 1596 find the safest road【dijkstra & floyed & SPFA 】
- HDU 1596 find the safest road
- HDU 1596 --find the safest road
- 【最短路径-Dis】hdu 1596 find the safest road
- HDU 1596 find the safest road(类最短路径)
- 1596 hdu find the safest road
- hdu1596 find the safest road 最短路dijkstra
- find the safest road HDU - 1596 (spfa)
- HDU-1596-find the safest road
- hdu-1596 find the safest road(SPFA做法)
- HDU 1596 find the safest road