AIZU 2249 Road Construction

来源:互联网 发布:吉利康迪k17数据 编辑:程序博客网 时间:2024/05/06 01:35

Description

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
u
1 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, 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 31 2 1 22 3 2 13 1 3 25 51 2 2 22 3 1 11 4 1 14 5 1 15 3 1 15 101 2 32 101 3 43 431 4 12 521 5 84 232 3 58 422 4 86 992 5 57 833 4 11 323 5 75 214 5 23 435 101 2 1 531 3 1 651 4 1 241 5 1 762 3 1 192 4 1 462 5 1 253 4 1 133 5 1 654 5 1 340 0

Output for the Sample Input

35137218


给出m条路 从中选路来建 每条路有为 u点到v点 长度为w 花费为c 
要求建完后任意两点都联通  
且在首都到各个城市的最短距离不变的前提下 !!!!
求最小花费


一开始看求最小花费 就想着最小生成树了 其实还要最短距离不变。。。。
要用dijkstra 


<span style="font-size:18px;">#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <vector>#include <cmath>#include <stack>#include <map>#include <set>#define pi acos(-1)#define LL long long#define INF 0x3f3f3f3fusing namespace std;typedef pair<int, int> P;const int maxn = 1e5 + 5;struct edge{    int v, w, c;    edge(int v_, int w_, int c_){        v = v_; w = w_; c = c_;    }};vector<edge>G[maxn];int n, m;int cnt[maxn];int dis[maxn];void dijkstra(int s){    priority_queue<P, vector<P>, greater<P> > q;    for (int i = 1; i <= n; i++)        dis[i] = INF;    dis[s] = 0;    q.push(P(dis[s], s));    while (!q.empty())    {        P cur = q.top(); q.pop();        int u = cur.second;        int w = cur.first;        if (dis[u] < w) continue;        for (int i = 0; i < G[u].size(); i++){            edge e = G[u][i];            if (dis[e.v] > dis[u] + e.w){                dis[e.v] = dis[u] + e.w;                cnt[e.v] = e.c;//  当前为最短距离 因此取此时花费                 q.push(P(dis[e.v], e.v));            }            else if (dis[e.v] == dis[u] + e.w) // 如果距离相同 取花费最小的                 cnt[e.v] = min(cnt[e.v], e.c);        }    }}int main(void){//freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin);    int i, j, ans, u, v, w, c;    while (cin >> n >> m && (n||m))    {        for (i = 1; i <= n; i++) G[i].clear();        for (i = 1; i <= m; i++){            scanf("%d %d %d %d", &u, &v, &w, &c);            G[u].push_back(edge(v, w, c));            G[v].push_back(edge(u, w, c));        }        dijkstra(1);        ans = 0;        for (i = 2; i <= n; i++)            ans += cnt[i];        cout << ans << endl;    }    return 0;}</span>


0 0
原创粉丝点击