1. 程式人生 > >AOJ 2249 Road Construction 迪傑斯特拉演算法

AOJ 2249 Road Construction 迪傑斯特拉演算法

Problem H:

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, viN , uivi , 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

題意:

求從1到其他點的最短路徑的最小花費。

思路:

就是在原來的迪傑斯特拉演算法上加上了個判斷。

開一個新的陣列, 這個陣列表示的是某條連線最短路集合中的某個點到此點的花費。

最後的結果只需要求和就可以了。

判斷方面, 如果滿足d[v]<d[u]+edge[u][v]或d[v]==d[u]+edge[u][v]&&sp[v]>sp[u][v],則更新 sp[v];

程式碼如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int maxn=10005;
const int INF=0x3f3f3f3f;
int n,m;
struct edge
{
    int e,len,sp;
};
vector <edge> ve[maxn];
int vis[maxn];
int d[maxn];
int sum;
int spp[maxn];
void init ()
{
    memset (vis,0,sizeof(vis));
    sum=0;
    for (int i=0;i<maxn;i++)
        ve[i].clear();
    for (int i=0;i<maxn;i++)
        {
            d[i]=INF;
            spp[i]=INF;
        }
    d[1]=0;
    spp[1]=0;
}
void djst ()
{
    while (1)
    {
        int maxx=INF;
        int u=-1;
        for (int i=1;i<=n;i++)
        {
            if(!vis[i]&&maxx>d[i])
            {
                maxx=d[i];
                u=i;
            }

        }
        if(u==-1)
            break;
        vis[u]=1;
        for (int i=0;i<ve[u].size();i++)
        {
            int v=ve[u][i].e;
            //分情況判斷
            if(!vis[v])
            {
                int temp=d[u]+ve[u][i].len;
                if(temp<d[v])
                {
                    d[v]=temp;
                    spp[v]=ve[u][i].sp;
                }
                else if(temp==d[v]&&spp[v]>ve[u][i].sp)
                {
                    spp[v]=ve[u][i].sp;
                }
            }
        }
    }
    for (int i=2;i<=n;i++)
        sum+=spp[i];
    printf("%d\n",sum);
}
int main()
{
    while (scanf("%d%d",&n,&m)!=EOF&&(n||m))
    {
        init();
        for (int i=0;i<m;i++)
        {
            int x,y,len,sp;
            scanf("%d%d%d%d",&x,&y,&len,&sp);
            edge temp1,temp2;
            temp1.e=y; temp1.len=len; temp1.sp=sp;
            temp2.e=x; temp2.len=len; temp2.sp=sp;
            ve[x].push_back(temp1);
            ve[y].push_back(temp2);
        }
        djst();
    }
    return 0;
}