单源最短路径+最小花费生成树

A - Road Construction
Time Limit:8000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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
1v1d1c1
.
.
.
uMvMdMcM

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 ≤ Nui ≠ 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 isdi 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最近,求最小花费;

我的做法是优先队列加dijstra算法,队列先按照距离排序,然后按照花费排序;

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int maxn=10050,maxm=20050,inf=0x3f3f3f3f;
struct Edge
{
    int to,next,dis,cos ;
}edge[maxm*2];
struct Pair
{
    int D,V,C;
    Pair(){;}
    Pair(int d,int c,int v)
    {
        D=d,V=v,C=c;
    }
    friend bool operator <(const Pair a,const Pair b)
    {
        if(a.D!=b.D)
        return a.D>b.D;
        else return a.C>b.C;
    }
};
int tot,head[maxn],dist[maxn],cost[maxn];
bool vis[maxn];
void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
}
void add_edge(int u,int v,int d,int c)
{
    edge[tot].to=v;
    edge[tot].dis=d;
    edge[tot].cos=c;
    edge[tot].next=head[u];
    head[u]=tot++;
}
int dijstra(int start)
{
    memset(vis,false,sizeof(vis));
    memset(dist,inf,sizeof(dist));
    int ans=0;
    dist[start]=0;
   // vis[start]=true;
    priority_queue<Pair>que;
    que.push(Pair(0,0,start));
    while(!que.empty())
    {
        Pair now=que.top();
        que.pop();
        if(vis[now.V])
            continue;
        vis[now.V]=true;
        ans+=now.C;
        for(int i=head[now.V];i!=-1;i=edge[i].next)
        {
            int to=edge[i].to;
            que.push(Pair(now.D+edge[i].dis,edge[i].cos,to));
        }
    }
    return ans;
}
int main()
{
    int u,v,d,c;
    int n,m;
    while(scanf("%d%d",&n,&m)!=-1)
    {
        init();
        if(n==0&&m==0)break;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d%d",&u,&v,&d,&c);
            add_edge(u,v,d,c);
            add_edge(v,u,d,c);
        }
        printf("%d\n",dijstra(1));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值