AOJ 2249 Road Construction Dijkstra变种

题目链接

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
u1 v1 d1 c1
.
.
.
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, ui, vi, di and ci (1 ≤ ui, vi ≤ N , ui ≠ vi , 1 ≤ di ≤ 1000, 1 ≤ ci ≤ 1000). ui , vi, di 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

题目大意:
计划图中有n个城市m条路,首都是1号城市,要选一些路去修,让各个城市到首都的路径最短,在路径最短的情况下修路费用最小。

解题思路:
题意读懂之后,会发现这是一个单源最短路问题,有一点需要注意的是题目要求在路径最短的情况下修路费用最小。考虑Dijkstra算法的过程,每次向已经标记的那些点形成的连通图中加入新的newnode时,实际上只引入了一条边,把newnode加进连通图的费用也只是这条边的费用
对Dijkstra稍作修改,如果路径同样小,取费用更小的。这里的费用是把这个点加进连通图的费用,而不是修出整条路经的费用,因为之前的那些路的费用其实已经计算过了。
核心代码:

if (d[to] > d[newnode] + len || (d[to] == d[newnode] + len && cost[to] > c)) {//核心判断
	d[to] = d[newnode] + len;
	cost[to] = c;
}

AC代码:

//迪杰斯特拉算法
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define maxn 10001
struct node {
	int v, l, w;
};
vector<node> E[maxn];
int n, m;
#define INF 0x3fffffff
int d[maxn];
int cost[maxn];//用于保存将点加进连通图的最小费用
bool mark[maxn];
void init() {
	for (int i = 1; i <= n; i++) {
		E[i].clear();//注意清空之前的边信息!!!
		mark[i] = false; d[i] = INF; cost[i] = INF;
	}
}
void dj(int s) {
	d[s] = 0; mark[s] = true; cost[s] = 0;
	int newnode = s; 
	for (int i = 1; i < n; i++) {//循环n-1次
		for (int j = 0; j < E[newnode].size(); j++) {
			int to = E[newnode][j].v;
			int len = E[newnode][j].l;
			int c = E[newnode][j].w;
			if (mark[to] == true) continue;
			if (d[to] > d[newnode] + len || (d[to] == d[newnode] + len && cost[to] > c)) {//核心判断
				d[to] = d[newnode] + len;
				cost[to] = c;
			}
		}
		int tmp = INF;
		for (int i = 1; i <= n; i++) {
			if (mark[i] || d[i] == INF) continue;
			if (d[i] < tmp) {
				tmp = d[i];
				newnode = i;
			}
		}
		mark[newnode] = true;
	}
}
int main() {
	while (cin >> n >> m) {
		if (n == 0 && m == 0) break;
		init();
		int u, v, l, w;
		for (int i = 1; i <= m; i++) {
			cin >> u >> v >> l >> w;
			node tmp;
			tmp.v = v; tmp.l = l; tmp.w = w;
			E[u].push_back(tmp);//双向边
			tmp.v = u;
			E[v].push_back(tmp);
		}
		dj(1);
		int ans = 0;
		for (int i = 1; i <= n; i++) {
			//cout << i << " " << cost[i] << endl;
			ans += cost[i];
		}
		cout << ans << endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值