当前位置:网站首页>【英雄哥六月集训】第 27天: 图
【英雄哥六月集训】第 27天: 图
2022-07-27 10:01:00 【如果我是温帅帅】
系列文章
图
图通常用来表示和存储具有“多对多”关系的数据,是数据结构中非常重要的一种结构。
一、 概率最大的路径
class Solution {
int maxn = 10010;
List<List<Edge>> edge = new ArrayList<List<Edge>>(maxn);
class Edge{
int to;
double val;
Edge(){
}
Edge(int t,double v){
this.to=t;
this.val=v;
}
}
public double bfs(int start,int end){
double[] dis = new double[maxn];
int i;
for(i=0;i<maxn;++i){
dis[i]=0.00d;
}
dis[start]=1.00d;
Queue<Integer> q = new ArrayDeque<Integer>();
q.add(start);
while(!q.isEmpty()){
Integer u = q.remove();
int len = edge.get(u).size();
for(i=0;i<len;++i){
int v = edge.get(u).get(i).to;
double d = dis[u]*(edge.get(u).get(i).val);
if(d>dis[v]){
dis[v]=d;
q.add(v);
}
}
}
return dis[end];
}
public double maxProbability(int n, int[][] edges, double[] succProb, int start, int end) {
int i;
for(i=0;i<maxn;++i){
edge.add(new ArrayList<Edge>());
}
for(i=0;i<edges.length;++i){
if(succProb[i]<=0){
continue;
}
int u = edges[i][0];
int v = edges[i][1];
double w = succProb[i];
edge.get(u).add(new Edge(v,w));
edge.get(v).add(new Edge(u,w));
}
return bfs(start,end);
}
}
二、 信物传送
class Solution {
int[][] dir = new int[][]{
{
-1,0},{
0,1},{
1,0},{
0,-1}
};
public int conveyorBelt(String[] matrix, int[] start, int[] end) {
int i,j;
int m=matrix.length;
int n = matrix[0].length();
int[][] grid = new int [110][110];
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(matrix[i].charAt(j)=='^'){
grid[i][j]=0;
}
else if(matrix[i].charAt(j)=='>'){
grid[i][j]=1;
}
else if(matrix[i].charAt(j)=='v'){
grid[i][j]=2;
}
else if(matrix[i].charAt(j)=='<'){
grid[i][j]=3;
}
}
}
int u = start[0]*n+start[1];
int[] dis = new int[10010];
for(i=0;i<n*m;++i){
dis[i]=100000000;
}
dis[u]=0;
Deque<Integer> q = new ArrayDeque<Integer>();
q.add(u);
while(!q.isEmpty()){
Integer f = q.remove();
int x = f/n;
int y = f%n;
for(i=0;i<4;++i){
int tx = x+dir[i][0];
int ty = y+dir[i][1];
if(tx<0||ty<0||tx==m||ty==n){
continue;
}
int next = tx*n+ty;
int nextd = dis[f]+((grid[x][y]==i?0:1));
if(nextd < dis[next]){
dis[next] = nextd;
q.add(next);
}
}
}
return dis[end[0]*n+end[1]];
}
}
三、 T 秒后青蛙的位置
class Solution {
int maxn = 110;
List<List<Integer>> e = new ArrayList<List<Integer>>(maxn);
double ans = 0.00d;
public void inite(){
for(int i=0;i<maxn;i++){
e.add(new ArrayList<Integer>());
}
}
public void dfs(int u,int fat,double p,int step,int maxstep,int target){
int i,cnt=0;
for(i =0; i<e.get(u).size(); ++i){
if(e.get(u).get(i)!=fat){
++cnt;
}
}
if(step <= maxstep){
if(u==target){
if(cnt ==0 && step <= maxstep){
ans = p;
} else if(cnt>=1 && step == maxstep){
ans = p;
}
}
}
if(step == maxstep){
return ;
}
for(i=0; i<e.get(u).size(); ++i){
if(e.get(u).get(i)!=fat){
dfs(e.get(u).get(i),u,p*1.0/cnt,step+1,maxstep,target);
}
}
}
public double frogPosition(int n, int[][] edges, int t, int target) {
inite();
int i;
for(i=0; i<edges.length; ++i){
int u = edges[i][0];
int v = edges[i][1];
e.get(u).add(v);
e.get(v).add(u);
}
dfs(1,0,1,0,t,target);
return ans;
}
}
总结
边栏推荐
- 达梦 PARTGROUPDEF是自定义的对象吗?
- Pyautogui实现自动化办公-RPA小case
- NFT system development - Tutorial
- Pygame: alien invasion
- Ant advanced -path and fileset
- 超赞的卡尔曼滤波详解文章
- 文件上传漏洞绕过方法
- Case of burr (bulge) notch (depression) detection of circular workpiece
- 学习Typescript(一)
- Leetcode.565. array nesting____ Violent dfs- > pruning dfs- > in situ modification
猜你喜欢

Shell integrated application cases, archiving files, sending messages

卸载CUDA11.1

Switch port mirroring Configuration Guide

Metasploit-永恒之蓝攻击

Food safety | the more you eat junk food, the more you want to eat it? Please keep this common food calorimeter
![[SCM]源码管理 - perforce 分支的锁定](/img/c6/daead474a64a9a3c86dd140c097be0.jpg)
[SCM]源码管理 - perforce 分支的锁定

LeetCode.814. 二叉树剪枝____DFS

Uninstall cuda11.1

线代004

Interview JD T5, was pressed on the ground friction, who knows what I experienced?
随机推荐
Discussion on a problem
Ant高级-task
Xiandai 004
直播倒计时 3 天|SOFAChannel#29 基于 P2P 的文件和镜像加速系统 Dragonfly
Anaconda installation (very detailed)
LeetCode.814. 二叉树剪枝____DFS
TFlite 的简单使用
Girl fan wants to find a boyfriend, but it's for
NVIDIA geforce experience login error: the verifier failed to load. Please check your browser settings, such as the advertisement interceptor (solution)
Matlab-绘制日期和持续时间图
When I went to oppo for an interview, I got numb
Qt 学习(二) —— .pro文件详解
Mathematical reasoning: five couples get together and shake hands when they meet
About new_ Online_ Judge_ 1081_ Thoughts on Goldbach's conjecture
活体检测综述
Reason for pilot importerror: cannot import name 'pilot_ Version 'from' PIL ', how to install pilot < 7.0.0
Uninstall cuda11.1
3D restoration paper: shape painting using 3D generative advantageous networks and recurrent revolutionary networks
QT learning (II) -.Pro file explanation
StyleGAN论文笔记+修改代码尝试3D点云生成