Aizu 2249Road Construction 单源最短路变形 spfa模板改写

来源:互联网 发布:软件功能测试报告模板 编辑:程序博客网 时间:2024/04/29 23:02

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. Thei-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 connectsui-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 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


卡了将近三个小时的题==

建全国的路的一部分,依旧要求联通,但是首都到各个城市的最短距离不变的前提下求最小花费

开始就一直纠结于要以“最小花费为中心”要怎么表示出某边是最短路径的边,不可删的问题,然后换两种最小生成树无果,搜题解,说是spfa最短路变形,松弛操作限制花费最小即可。然后模板受限制,所以还是要写邻接表==不能图省事用vector

现在想来,为什么可以不针对“最小花费”这个条件呢?因为spfa是“单源最短路”!完美限制了最短距离不变的要求,当距离相同选择最小花费即可

    #include <stdio.h>    #include <string.h>    #include <queue>    #include <iostream>    #include <algorithm>    using namespace std;    const int maxn=10050;    const int maxe=2005000;    const int INF=1e9;    struct note    {        int to;        int w;        int c;        int next;    };    note edge[maxe];    int head[maxn];    int ip;    void init()    {        memset(head,-1,sizeof(head));        ip=0;    }    void addedge(int u,int v,int w,int c)    {        edge[ip].to=v;        edge[ip].c=c;        edge[ip].w=w;        edge[ip].next=head[u];        head[u]=ip++;    }    int cost[maxn];    int dis[maxn];    bool vis[maxn];    queue<int>q;    void spfa(int s,int n)    {        while(!q.empty())q.pop();        for(int i=1;i<=n;i++)            cost[i]=dis[i]=INF;        memset(vis,false,sizeof(vis));        dis[s]=0;        cost[s]=0;        vis[s]=true;        q.push(s);        while(!q.empty())        {            int u=q.front();            q.pop();            vis[u]=false;            for(int i=head[u];i!=-1;i=edge[i].next)            {                int to=edge[i].to;                int val=edge[i].w;                if(dis[to]>dis[u]+val||(dis[to]==dis[u]+val&&cost[to]>edge[i].c))//有多条最短路时,取花费最小的                {                    dis[to]=dis[u]+val;                    cost[to]=edge[i].c;                    if(!vis[to])                    {                        q.push(to);                        vis[to]=true;                    }                }            }        }    }    int main()    {       // freopen("cin.txt","r",stdin);        int n,m;        int u,v,w,c;        while(~scanf("%d%d",&n,&m))        {            if(n==0&&m==0) break;            init();            for(int i=0;i<m;i++)            {                scanf("%d%d%d%d",&u,&v,&c,&w);                addedge(u,v,c,w);                addedge(v,u,c,w);            }            spfa(1,n);            int ans=0;            for(int i=1;i<=n;i++)                ans+=cost[i];            printf("%d\n",ans);        }        return 0;    }


0 0
原创粉丝点击