重载疑问
查看原帖
重载疑问
707007
allowing楼主2024/7/11 10:15

一共两份代码 A1:

#include <iostream>
#include <queue>
using namespace std;

const int N = 2e5 + 10;
struct edge{
	int sec, nxt;
	// 1 -> 4, 1 -> 7, 1- > 5, 1 -> 8
	// 1--4--7--5--8
	// 1 -> first, 4 --> sec(ond)
	// 4 -> first, 7 --> sec(ond)
	// sec 表示边x->y中的y
	// nxt表示与a相连的下标
	double dis; // 1.0 - z% 
}e[N];
int head[N];
double dis[N];
bool vis[N];//判断走没走过
int n, m, s, idx, a, b; 

void addedge(int x, int y, int z) {
	e[++idx].dis = 1.0 - z / 100.0;
	e[idx].sec = y;
	e[idx].nxt = head[x];
	head[x] = idx;
}

struct Node{
	double dis;
	int pos;
	bool operator<(const Node &P)const{
		return P.dis > dis;
	}
};

void Dijkstra() {
	dis[a] = 1.0;
	priority_queue<Node> Q;
	Q.push(Node{1.0, a});
	
	while (!Q.empty()) {
		Node tmp = Q.top();
		Q.pop();
		
		int x = tmp.pos;
		if (vis[x]) continue;
		vis[x] = true;
		
		//       当前头结点  最后是0  找下一个点 
		for (int i = head[x]; i; i = e[i].nxt) {
			int y = e[i].sec;
			
			if (dis[y] < dis[x] * e[i].dis) {
				dis[y] = dis[x] * e[i].dis;
				if (!vis[y]) Q.push(Node{dis[y], y});
			}
		}
	}
}
int main(){
	scanf ("%d %d", &n, &m);
	for (int i = 0; i < m; i++) {
		int x, y, z;
		scanf ("%d %d %d", &x, &y, &z);
		addedge(x, y, z);
		addedge(y, x, z);   // 反向建边  
	}
	scanf("%d %d", &a, &b);
	
	Dijkstra();
	
	printf("%.8lf", 100.0 / dis[b]);
	
	return 0;
}

A2:

#include <iostream>
#include <queue>
using namespace std;

const int N = 2e5 + 10;
struct edge{
	int sec, nxt;
	// 1 -> 4, 1 -> 7, 1- > 5, 1 -> 8
	// 1--4--7--5--8
	// 1 -> first, 4 --> sec(ond)
	// 4 -> first, 7 --> sec(ond)
	// sec 表示边x->y中的y
	// nxt表示与a相连的下标
	double dis; // 1.0 - z% 
}e[N];
int head[N];
double dis[N];
bool vis[N];//判断走没走过
int n, m, s, idx, a, b; 

void addedge(int x, int y, int z) {
	e[++idx].dis = 1.0 - z / 100.0;
	e[idx].sec = y;
	e[idx].nxt = head[x];
	head[x] = idx;
}

struct Node{
	double dis;
	int pos;
	bool operator<(const Node &P)const{
		return dis > P.dis;
	}
};

void Dijkstra() {
	dis[a] = 1.0;
	priority_queue<Node> Q;
	Q.push(Node{1.0, a});
	
	while (!Q.empty()) {
		Node tmp = Q.top();
		Q.pop();
		
		int x = tmp.pos;
		if (vis[x]) continue;
		vis[x] = true;
		
		//       当前头结点  最后是0  找下一个点 
		for (int i = head[x]; i; i = e[i].nxt) {
			int y = e[i].sec;
			
			if (dis[y] < dis[x] * e[i].dis) {
				dis[y] = dis[x] * e[i].dis;
				if (!vis[y]) Q.push(Node{dis[y], y});
			}
		}
	}
}
int main(){
	scanf ("%d %d", &n, &m);
	for (int i = 0; i < m; i++) {
		int x, y, z;
		scanf ("%d %d %d", &x, &y, &z);
		addedge(x, y, z);
		addedge(y, x, z);   // 反向建边  
	}
	scanf("%d %d", &a, &b);
	
	Dijkstra();
	
	printf("%.8lf", 100.0 / dis[b]);
	
	return 0;
}

两份代码的区别就是第32行的

  1. P.dis > dis
  2. dis > P.dis

1是AC, 但2是全WA,是什么原因啊

2024/7/11 10:15
加载中...