码迷,mamicode.com
首页 > 其他好文 > 详细

Aizu 2249Road Construction 单源最短路变形《挑战程序设计竞赛》模板题

时间:2017-08-05 16:45:57      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:include   ras   original   read   expec   竞赛   cost   acm   ++   

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到其他个点的距离最短的情况下,最少的花费是多少.

解题思路:

又把《挑战程序设计竞赛》上最短路章节好好看了遍,发现这题把板子变一点点就可以了 TwT,训练赛的时候听说同级有人用vector写出来了,太强辣!!

AC代码:

#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <string.h>
#include <iostream>
#include<algorithm>
#include <queue>
#include <vector>
#include<string>
//******************************************************
#define lrt (rt*2)
#define rrt  (rt*2+1)
#define LL long long
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
#define exp 1e-8
const double temp=(1+sqrt(5))/2;
//***************************************************
#define eps             1e-8
#define inf             0x3f3f3f3f
#define INF             2e18
#define LL              long long
#define ULL             unsigned long long
#define PI              acos(-1.0)
#define pb              push_back
#define mk              make_pair

#define all(a)          a.begin(),a.end()
#define rall(a)         a.rbegin(),a.rend()
#define SQR(a)          ((a)*(a))
#define Unique(a)       sort(all(a)),a.erase(unique(all(a)),a.end())
#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(min(a,b),min(c,d))
#define max4(a,b,c,d)   max(max(a,b),max(c,d))
#define max5(a,b,c,d,e) max(max3(a,b,c),max(d,e))
#define min5(a,b,c,d,e) min(min3(a,b,c),min(d,e))
#define Iterator(a)     __typeof__(a.begin())
#define rIterator(a)    __typeof__(a.rbegin())
#define FastRead        ios_base::sync_with_stdio(0);cin.tie(0)
#define CasePrint       pc(‘C‘); pc(‘a‘); pc(‘s‘); pc(‘e‘); pc(‘ ‘); write(qq++,false); pc(‘:‘); pc(‘ ‘)
#define vi              vector <int>
#define vL              vector <LL>
#define For(I,A,B)      for(int I = (A); I < (B); ++I)
#define rFor(I,A,B)     for(int I = (A); I >= (B); --I)
#define Rep(I,N)        For(I,0,N)
using namespace std;
const int maxn=1e5+10;
typedef pair<int, int>P;
struct edge
{
    int to,d,cost;
};
int n,m,d[maxn],sum[maxn];
vector<edge>G[maxn];
void Dijkstra(int s)
{
    priority_queue<P,vector<P>,greater<P> > que;
    fill(d,d+n+1,inf);
    d[s]=0;
    que.push(P(0,s));
    while(!que.empty())
    {
        P p=que.top();  que.pop();
        int v=p.second;
        if(d[v]<p.first)    continue;
        for(int i=0;i<G[v].size();i++)
        {
            edge e=G[v][i];
            if(d[e.to]>=d[v]+e.d)
            {
                if(d[e.to]==d[v]+e.d)   sum[e.to]=min(sum[e.to],e.cost);
                else    sum[e.to]=e.cost;
            d[e.to]=d[v]+e.d;
            que.push(P{d[e.to],e.to});
            }
        }

    }
}
int main()
{
    while(cin>>n>>m)
    {
        if(m+n==0)  break;
        int u,v,dis,c;
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=n;i++)   G[i].clear();
        for(int i=0;i<m;i++)
        {
            scanf("%d %d %d %d",&u,&v,&dis,&c);
            G[u].push_back(edge{v,dis,c});
            G[v].push_back(edge{u,dis,c});
        }
        Dijkstra(1);
        int ans=0;
        for(int i=2;i<=n;i++)
        {
            ans+=sum[i];
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

Aizu 2249Road Construction 单源最短路变形《挑战程序设计竞赛》模板题

标签:include   ras   original   read   expec   竞赛   cost   acm   ++   

原文地址:http://www.cnblogs.com/weimeiyuer/p/7290509.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!