3424:Candies

3424:Candies

代码(WA 10次):

vector内是又申请好的空间的,但queue和stack一半是存储的地址

这道可以用弗洛伊德Prim的思路,但无法解决此问题,会照成内存过大,因此要取巧,用多少,申请多少。

#include<iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <functional>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <algorithm>
#define ll long long
#define mes(x,y) memset(x,y,sizeof(x))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const ll INFF = 2000000;
const ll maxn = 99999999;

ll n, m, k, i, j, vb;
struct node {
	ll end, value;
};
bool operator <(const node & n1,const node & n2) {
	return n1.value > n2.value;
}
priority_queue<node>pq;
bool flag[300040] = { 0 };
vector<vector<node>>v;
int main() {
	FAST_IO;
	while (cin >> n >> m) {
		node n1;
		mes(flag, 0);
		v.clear();
		v.resize(n + 1);
		ll x, y, z;
		for (i = 1; i <= m; i++) {
			cin >> x >> y >> z;
			n1.end = y;
			n1.value = z;
			v[x].push_back(n1);
		}
		n1.end = 1;
		n1.value = 0;
		pq.push(n1);
		while (!pq.empty()) {
			n1 = pq.top(); pq.pop();
			if (flag[n1.end])continue;
			flag[n1.end] = true;
			if (n1.end == n) {
				cout << n1.value << endl;
				break;
			}
			for (i = 0, j = v[n1.end].size(); i < j; i++) {
				node n2; n2.end = v[n1.end][i].end;
				if (flag[n2.end])continue;
				n2.value = v[n1.end][i].value + n1.value;
				pq.push(n2);
			}
		}
	}
}

发布了148 篇原创文章 · 获赞 7 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_44417851/article/details/104446908