find the safest road HDU杭电1596【Dijkstra || SPFA】
The HDU Find Dijkstra SPFA 杭电
2023-09-11 14:15:00 时间
http://acm.hdu.edu.cn/showproblem.php?pid=1596
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所在的城市和要去的城市
第一行: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
#include <cstdio> #include <cstring> #include <queue> #define MAXN 1100 using namespace std; double map[MAXN][MAXN]; int vis[MAXN];//推断是否增加队列了 int num; double low[MAXN];//存最短路径 int s, e; int M, N; void SPFA() { int i, j; queue<int> Q; memset(low, 0, sizeof(low)); memset(vis, 0, sizeof(vis)); vis[s] = 1; low[s] = 1; Q.push(s); while(!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = 0;//出队列了,不在队列就变成0 for(i = 1; i <= N; ++i) { if(low[i] < low[u] * map[u][i]) { low[i] = low[u] * map[u][i]; if(!vis[i]) { vis[i]=1; Q.push(i); } } } } if(low[e] == 0) printf("What a pity!\n"); else printf("%.3f\n",low[e]); } int main() { int u, v; double w; while(~scanf("%d", &N)) { num=0; for(int i=1 ;i <= N; ++i) { for(int j=1; j <= N; ++j) { scanf("%lf",&w); map[i][j]=w; } } scanf("%d", &M); while(M--) { scanf("%d%d", &s, &e); SPFA(); } } return 0; }
Dijkstra
#include<stdio.h> #include<string.h> double map[1010][1010]; double dis[1010]; bool used[1010]; int n; int i,j; void dijkstra(int u) { memset(used,0,sizeof(used)); for(i=1;i<=n;++i) dis[i]=0; int pos=u; for(i=1;i<=n;++i)//第一次给dis赋值 { dis[i]=map[u][i]; } dis[u]=1; used[u]=1; for(i=1;i<n;++i)//再找n-1个点 { double max=0; for(j=1;j<=n;++j) { if(!used[j]&&max<dis[j]) { max=dis[j]; pos=j; } } used[pos]=1; dis[pos]=max; for(j=1;j<=n;++j)//把dis数组更新,也叫松弛 { if(!used[j]&&dis[j]<map[pos][j]*dis[pos]) { dis[j]=map[pos][j]*dis[pos]; } } } } int main() { int m; double w; int u,v; while(~scanf("%d",&n)) { for(i=1;i<=n;++i) for(j=1;j<=n;++j) { scanf("%lf",&w); map[i][j]=w; } scanf("%d",&m); while(m--) { scanf("%d%d",&u,&v); dijkstra(u); if(dis[v]==0) printf("What a pity!\n"); else printf("%.3lf\n",dis[v]); } } return 0; } /* 3 1 0.5 0.5 0.5 1 0.2 0.5 0.2 1 3 1 2 2 3 1 3 */
相关文章
- 安卓 android studio 报错 The specified Android SDK Build Tools version (27.0.3) is ignored, as it is below the minimum supported version (28.0.3) for Android Gradle
- iOS开发之使用CocoaPods更新第三方出现“target overrides the `OTHER_LDFLAGS`……”问题解决方案
- Linux 将一般的用户加入sudo组is_not_in_the_sudoers_file._This_incident_will_be_reported解决方法
- SQL SERVER 2005删除维护作业报错:The DELETE statement conflicted with the REFERENCE constraint "FK_subplan_job_id"
- HDU 4762 Cut the Cake
- [转]HDFS Metadata Directories Explained - An overview of files and configurations under the HDFS meta-directory
- sharepoint2010:The number of items in this list exceeds the list view threshold, which is 20000 items.
- [React] Directly Dispatch Actions into the Redux Store Before React Renders
- [Express] Designing the Application to be Extensible
- [RxJS 6] The Catch and Rethrow RxJs Error Handling Strategy and the finalize Operator
- The CPU Costing Model: A Few Thoughts Part IV (Map of the Problematique)
- AndroidStudio3.0 注解报错Annotation processors must be explicitly declared now. The following dependencies on the compile classpath are found to contain annotation processor.
- Ubuntu报“xxx is not in the sudoers file.This incident will be reported” 错误解决方法
- Unexpected XML declaration. The XML declaration must be the first node in the document and no white
- 解决The type or namespace name 'XXXX' does not exist in the namespace 'XXXXXXXXX' 的错误
- NLP:LSTM之父眼中的深度学习十年简史《The 2010s: Our Decade of Deep Learning / Outlook on the 2020s》的参考文献
- 已解决FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future
- The current user does not have write permissions to the target environment.
- The difference between the request time and the current time is too large.阿里云oss上传图片报错
- hdu 1028 Ignatius and the Princess III
- HDU 1598 find the most comfortable road (最小生成树) >>
- hdu 4869 Turn the pokers
- 【问题解决】The connection to the server localhost:8080 was refused
- PAT 1069 The Black Hole of Numbers[简单]
- The value for the useBean class attribute is invalid.
- Your configuration specifies to merge with the ref
- 编译webrtc报错:ERROR: The installation of the Chrome OS default fonts failed.
- git push报错 fatal: The current branch master has no upstream branch. To push the current
- Accidental override: The following declarations have the same JVM signature (getWindow()Landroid/vie