文章

12

粉丝

0

获赞

12

访问

624

头像
概率最大的路径 题解:
P1927 复旦大学2022年机试题
发布于2026年2月26日 15:51
阅读数 17

#include<bits/stdc++.h>
using namespace std;
const int N = 1e4+10;
const int INF = 1e9;

struct node{
	int v;
	double p;
};
int n,m;
vector<node> a[N];
double dist[N];//从起点走到节点i的最大概率

void dijkstra(int s){
	for (int i = 0; i <= n; i++) {
        dist[i] = 0.0;
    }
	dist[s] = 1.0;
	priority_queue<pair<double,int>> pq;
	pq.push({1.0,s});
	while(!pq.empty()){
		double p = pq.top().first;
		int u = pq.top().second;
		pq.pop();
		
		if(p < dist[u]){
			continue;
		}
		
		for(int i = 0 ; i < a[u].size(); i++){
			int v = a[u][i].v;
			double w = a[u][i].p;
			if(dist[u]*w > dist[v]){
				dist[v] = dist[u] * w;
				pq.push({dist[v],v});
			}
		}
	}
	
}

int main(){
	
	cin>>n>>m;
	
	vector<pair<int,int>> temp_edge(m);
	for(int i = 0 ; i < m ; i++){
		cin>>temp_edge[i].first>>temp_edge[i].second;
	}
	for(int i = 0 ; i < m; i++){
		double curr_p;
		cin>>cu...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发