最短路+路径还原——Road Construction(Aizu - 2249)

最短路+路径还原

——Road Construction(Aizu - 2249)
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.

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

解析:
首先,用Dijkstra算法来计算最短路径(虽然题目有点像最小生成树,但是题目第一关键字是最小路径,在路径相同的情况下选择最小费用),再通过pre数组进行一个路径还原。
上面都是废话,重点在于这个pre数组,放的不是前一个端点的标号,而是链接两个顶点的路径的费用。最后,将所有的pre加在一起,就是将所有费用和在一起(这里因为所有顶点和图中其他点肯定有一条边相连,就是一棵树有n-1条边)。
注意点:1.pre[1]=0;
2.神坑:就是1 0这种可能是存在的所以只能&&n

#include<cstdio>
#include<string.h>
#include<queue>
#include<vector>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1e5,maxm=1e5;
int n,m;
struct edge{
    int to,dist,cost;
};
typedef pair<int,int> P;
int d[maxn];
int pre[maxn];
int main()
{
    int a,b,c,e;
    edge e1,e2;
    while(~scanf("%d%d",&n,&m)&&n)
    {
       vector<edge> G[maxn];
       for(int i=0;i<m;i++){
            scanf("%d%d%d%d",&a,&b,&c,&e);
            e1.cost=e2.cost=e;e1.dist=e2.dist=c;e1.to=b;e2.to=a;
            G[a].push_back(e1);G[b].push_back(e2);
        }
    priority_queue<P,vector<P>,greater<P> >que;
    fill(d+1,d+n+1,inf);
    d[1]=0;
    que.push(P(0,1));
    pre[1]=0;      //每个图n-1条边
    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.dist){
                d[e.to]=d[v]+e.dist;
                pre[e.to]=e.cost;  //每个pre放了这个点与这个图相连的那条边,这样后面就不用回溯找边的cost值
                que.push(P(d[e.to],e.to));
            }
            else if(d[e.to]==d[v]+e.dist){
                pre[e.to]=min(e.cost,pre[e.to]);
            }
        }
    }
        int ans=0;
        for(int i=1;i<=n;i++){
            ans+=pre[i]; 
            }
        printf("%d\n",ans);
     }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值