求大佬解答
查看原帖
求大佬解答
940317
lightme楼主2024/3/1 23:26

链式前向星+dij+优先队列,过样例但是全wa

// Problem: 
//     P1576 最小花费
//   
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1576
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>

#define ll long long 
#define endl '\n'
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>(b);i--)
#define N 2010 //1e6+100 
#define M 100010 
using namespace std;
int n,m,cnt;
struct node{
	int to,next,w;
}edge[M];
struct point{
	int vex;
	double value;
	friend bool operator<(point a,point b){
		return a.value>b.value;
	}
};

priority_queue<point> q;
const int INF= 0x3f3f3f3f;
int head[N],a,b;
double dis[N];
bool vis[N];


void init(){
	cnt=0;
	memset(head,-1,sizeof head);
}
void add_edge(int u,int v,int w){
	edge[cnt].w=w;
	edge[cnt].to=v;
	edge[cnt].next=head[u];
	head[u]=cnt++;
}
double clac(double d,double z){
	return d/(100-z);
}
void dijkstra(int start){
	rep(i,1,n) dis[i]=INF;
	dis[start]=10000;
	point p;
	p.vex=start;
	p.value=10000;
	q.push(p);
	while(!q.empty()){
		int u=q.top().vex;
		q.pop();
		if(vis[u])continue;
		vis[u]=true;
		
		for(int i=head[u];i!=-1;i=edge[i].next){
			int to=edge[i].to;
			if(!vis[to] && dis[to]>clac(dis[u],edge[i].w)){
				dis[to]=clac(dis[u],edge[i].w);
				p.vex=to;
				p.value=dis[to];
				q.push(p);
			}

		}
	}
	printf("%.8lf",dis[b]*100);
}

int main(){
	//ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	init();
	int x,y,z;
	rep(i,1,m){
		cin>>x>>y>>z;
		add_edge(x,y,z);
		add_edge(y,x,z);
	}
	cin>>a>>b;
	dijkstra(a);
	return 0; 
} 
2024/3/1 23:26
加载中...