Road Construction Aizu - 2249 (最短路多权重)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/alex1997222/article/details/89159345

King Mercer is the king of ACM kingdom. There are one capital and some cities in his kingdom. Amazingly, there are no roads in the kingdom now. Recently, he planned to construct roads between the capital and the cities, but it turned out that the construction cost of his plan is much higher than expected.

In order to reduce the cost, he has decided to create a new construction plan by removing some roads from the original plan. However, he believes that a new plan should satisfy the following conditions:

  • For every pair of cities, there is a route (a set of roads) connecting them.
  • The minimum distance between the capital and each city does not change from his original plan.

Many plans may meet the conditions above, but King Mercer wants to know the plan with minimum cost. Your task is to write a program which reads his original plan and calculates the cost of a new plan with the minimum cost.

Input

The input consists of several datasets. Each dataset is formatted as follows.

N M
uvdc
.
.
.
uM vM dM cM 

The first line of each dataset begins with two integers, N and M (1 ≤ N ≤ 10000, 0 ≤ M ≤ 20000). N and M indicate the number of cities and the number of roads in the original plan, respectively.

The following M lines describe the road information in the original plan. The i-th line contains four integers, uividi and ci (1 ≤ uivi ≤ N , ui ≠ vi , 1 ≤ di≤ 1000, 1 ≤ ci ≤ 1000). ui , vidi and ci indicate that there is a road which connects ui-th city and vi-th city, whose length is di and whose cost needed for construction is ci.

Each road is bidirectional. No two roads connect the same pair of cities. The 1-st city is the capital in the kingdom.

The end of the input is indicated by a line containing two zeros separated by a space. You should not process the line as a dataset.

Output

For each dataset, print the minimum cost of a plan which satisfies the conditions in a line.

Sample Input

3 3
1 2 1 2
2 3 2 1
3 1 3 2
5 5
1 2 2 2
2 3 1 1
1 4 1 1
4 5 1 1
5 3 1 1
5 10
1 2 32 10
1 3 43 43
1 4 12 52
1 5 84 23
2 3 58 42
2 4 86 99
2 5 57 83
3 4 11 32
3 5 75 21
4 5 23 43
5 10
1 2 1 53
1 3 1 65
1 4 1 24
1 5 1 76
2 3 1 19
2 4 1 46
2 5 1 25
3 4 1 13
3 5 1 65
4 5 1 34
0 0

Output for the Sample Input

3
5
137
218

解题思路:

扫描二维码关注公众号,回复: 5831163 查看本文章

题目的意思是,给定若干个结点和若干条边,每条边边权有长度和花费,问怎样建路,在保持每个结点与起点的最小距离不变的情况下,

使花费最少?

这是一道典型的多边权问题,我们利用dijkstra算法,不断更新每个结点的最短路径和花费,如果在算法执行的过程中,一个结点到另一个结点的长度和结点当前的长度相等,则比较结点的花费,如果花费更小,则更新结点的花费。

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string.h>
#include <math.h>
using namespace std;
const int INF = 0x3fffffff;
const int MAXN = 10010;
const int MAXE = 20010;
struct stedge {
	int to,cLength,ccost;   //到达点,长度,花费价格
};
typedef pair<int, int> P;

int N, M;
vector<stedge> sG[MAXN];   
int sD[MAXN];
int cosD[MAXN];

void sDijkstra(int s) {
	priority_queue<P, vector<P>, greater<P> > que;
	fill(sD, sD + MAXN, INF);  //全部初始化为INF
	fill(cosD, cosD + MAXN, INF);
	sD[s] = 0;
	cosD[s] = 0;  //初始化
	que.push(P(0,s));  //把起点加入队列
	while (!que.empty()) {
		P p = que.top();
		que.pop();
		int v = p.second;
		if(sD[v] < p.first) continue;  //去掉旧的优化的边
		for (int i = 0; i < sG[v].size(); i++) {
			stedge e = sG[v][i];
			if (sD[e.to] >= sD[v] + e.cLength) {
				if (sD[e.to] > sD[v] + e.cLength) {
					sD[e.to] = sD[v] + e.cLength; 
					cosD[e.to] = e.ccost;
					que.push(P(sD[e.to], e.to));
				}
				else if (sD[e.to] == sD[v] + e.cLength) {
					if (cosD[e.to] > e.ccost) {
						cosD[e.to] = e.ccost;
					}
				}
			}
 		}
	}
}

int main() {

	while (scanf("%d %d", &N, &M) != EOF) {
		if(N == 0 && M == 0) break;
		memset(sG, 0, sizeof(sG));
		int nx, ny, nl, nc;
		for (int i = 0; i < M; ++i) {
			scanf("%d %d %d %d", &nx, &ny, &nl, &nc);
			sG[nx].push_back({ ny,nl,nc });
			sG[ny].push_back({ nx,nl,nc });
		}
		sDijkstra(1);
		int minstcos = 0;
		for (int v = 1; v <= N; ++v) {
			if (cosD[v] != INF) {
				minstcos += cosD[v];
			}
		}

		printf("%d\n", minstcos);
	}

	system("PAUSE");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/alex1997222/article/details/89159345
今日推荐