51nod 1443 路径和树 (最短路+最小生成树)

给定一幅无向带权连通图G = (V, E) (这里V是点集,E是边集)。从点u开始的最短路径树是这样一幅图G1 = (V, E1),其中E1是E的子集,并且在G1中,u到所有其它点的最短路径与他在G中是一样的。

现在给定一幅无向带权连通图G和一个点u。你的任务是找出从u开始的最短路径树,并且这个树中所有边的权值之和要最小。

 

Input

单组测试数据。
第一行有两个整数n和m(1 ≤ n ≤ 3*10^5, 0 ≤ m ≤ 3*10^5),表示点和边的数目。
接下来m行,每行包含3个整数 ui, vi, wi ,表示ui和vi之间有一条权值为wi的无向边(1 ≤ ui,vi ≤ n, 1 ≤ wi ≤ 10^9)。
输入保证图是连通的。
最后一行给出一个整数u (1 ≤ u ≤ n),表示起点。

Output

输出这棵树的最小的权值之和。

Input示例

3 3
1 2 1
2 3 1
1 3 2
3

Output示例

2

题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1443

通常情况下,给定一个u求单源最短路,得到的就是一颗最短路径树,但是也有不通常的情况,假如一个点u到v的最短路径不唯一呢,而且我们求树,又需要经过所有的点,这时候,我们就需要所有的最短路边,用最短路的算法去获取所有最短路的边,然后用最小生成树跑出最短路径树。

#pragma GCC optimize(2)
#include
#include
#include
#include
using namespace std;
namespace fastIO {
#define BUF_SIZE 100000
	//fread -> read
	bool IOerror = 0;
	inline char nc() {
		static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
		if (p1 == pend) {
			p1 = buf;
			pend = buf + fread(buf, 1, BUF_SIZE, stdin);
			if (pend == p1) {
				IOerror = 1;
				return -1;
			}
		}
		return *p1++;
	}
	inline bool blank(char ch) {
		return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
	}
	inline void read(int &x) {
		char ch;
		while (blank(ch = nc()));
		if (IOerror) return;
		for (x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0');
	}
#undef BUF_SIZE
};
using namespace fastIO;
const int maxn = 3e5 + 10;
const int inf = 0x3f3f3f3f;
typedef long long ll;
ll dis[maxn], f[maxn];
bool vis[maxn];
int n, m;
vector >vec[maxn];
struct node
{
	int u, v;
	ll cost;
	node(int U = -1, int V = -1, ll C = -1)
	{
		u = U;
		v = V;
		cost = C;
	}
	bool operator < (const node &x)
	{
		return cost < x.cost;
	}
};
vectoredge;
void spfa(int st)
{
	queuepq;
	memset(vis, false, sizeof(vis));
	memset(dis, inf, sizeof(dis));
	
	vis[st] = true;
	dis[st] = 0;
	pq.push(st);
	while (!pq.empty())
	{
		int u = pq.front();
		pq.pop();
		vis[u] = false;
		for (int i = 0; i < (int)vec[u].size(); i++)
		{
			int v = vec[u][i].first;
			ll cost = vec[u][i].second;
			if (dis[v] > dis[u] + cost)
			{
				dis[v] = dis[u] + cost;
				if (!vis[v])
				{
					vis[v] = true;
					pq.push(v);
				}
			}
		}
	}
}
int find(int x)
{
	if (x == f[x])
	{
		return x;
	}
	else
	{
		return f[x] = find(f[x]);
	}
}
void merge(int u, int v)
{
	u = find(u);
	v = find(v);
	if (u == v)
	{
		return;
	}
	else
	{
		f[u] = v;
	}
}
ll kur()
{
	ll ans = 0;
	memset(vis, false, sizeof(vis));
	sort(edge.begin(), edge.end());
	for (int i = 0; i < (int)edge.size(); i++)
	{
		int u = edge[i].u;
		int v = edge[i].v;
		ll w = edge[i].cost;
		if (vis[v] || find(u) == find(v))
		{
			continue;
		}
		vis[v] = true;
		merge(u, v);
		ans += w;
	}
	return ans;
}
int main()
{
	//freopen("C://input.txt", "r", stdin);
	read(n), read(m);
	for (int i = 0; i <= n; i++)
	{
		vec[i].clear();
		f[i] = i;
	}
	for (int i = 1; i <= m; i++)
	{
		int u, v, w;
		read(u), read(v), read(w);
		vec[u].push_back(make_pair(v, (ll)w));
		vec[v].push_back(make_pair(u, (ll)w));
	}
	int st;
	read(st);
	spfa(st);
	edge.clear();
	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j < (int)vec[i].size(); j++)
		{
			int v = vec[i][j].first;
			ll w = vec[i][j].second;
			if (dis[v] == dis[i] + w)
			{
				edge.push_back(node(i, v, w));
			}
		}
	}
	printf("%lld\n", kur());
	return 0;
}

 

你可能感兴趣的:(最短路,最小生成树)