ZOJ3946 Highway Project 最短路优化

Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can reach other cities from the capital as fast as possible. Thus, he proposed the highway project.
The Marjar Empire has N cities (including the capital), indexed from 0 to N - 1 (the capital is 0) and there are M highways can be built. Building the i-th highway costs Ci dollars. It takes Di minutes to travel between city Xi and Yi on the i-th highway.
Edward wants to find a construction plan with minimal total time needed to reach other cities from the capital, i.e. the sum of minimal time needed to travel from the capital to city i (1 ≤ i ≤ N). Among all feasible plans, Edward wants to select the plan with minimal cost. Please help him to finish this task.

Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first contains two integers N, M (1 ≤ N, M ≤ 105).
Then followed by M lines, each line contains four integers Xi, Yi, Di, Ci (0 ≤ Xi, Yi < N, 0 < Di, Ci < 105).

Output
For each test case, output two integers indicating the minimal total time and the minimal cost for the highway project when the total time is minimized.

Sample Input

2
4 5
0 3 1 1
0 1 1 1
0 2 10 10
2 1 1 1
2 3 1 2
4 5
0 3 1 1
0 1 1 1
0 2 10 10
2 1 2 1
2 3 1 2

Sample Output

4 3
4 4

给出一组边,要求从首都到所有城市的时间之和最短的情况下,花费最少。
先确定最短路,在得到最短路的情况下确定花费最少,也就是优先选择时间花费最少的,在时间花费最少的情况下优先选择最便宜的。
Dijsktra堆优化。在使用堆优化的时候确保没有大量的重边,否则可能还不如不优化。


#include <stdio.h>
#include <climits>
#include <cstring>
#include <time.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <string>

#define MAXN 100005
#define INF 1e11
#define ll long long
#define Pair pair<int,int>
#define re return

#define getLen(name,index) name[index].size()
#define mem(a,b) memset(a,b,sizeof(a))
#define Make(a,b) make_pair(a,b)
#define Push(num) push_back(num)
#define rep(index,star,finish) for(register int index=star;index<finish;index++)
#define drep(index,finish,star) for(register int index=finish;index>=star;index--)
using namespace std;
struct Edge{
    int finish;
    int d,c;
};
struct node{
    int index;
    ll dis;
};
bool operator <(const node &a,const node &b){
    re a.dis>b.dis;
}

int N,M;
ll dis[MAXN];
int cost[MAXN];
bool vis[MAXN];
vector<Edge> adj[MAXN];
priority_queue<node> Q;
void Dijkstra();
int main(){
    int _;
    scanf("%d",&_);
    while(_--){
        scanf("%d %d",&N,&M);
        rep(i,0,M){
            int star,finish,d,c;
            scanf("%d %d %d %d",&star,&finish,&d,&c);
            adj[star].push_back((Edge){finish,d,c});
            adj[finish].push_back((Edge){star,d,c});
        }

        Dijkstra();
        ll sumT=0,sumC=0;
        rep(i,1,N){
            sumT+=dis[i];
            sumC+=cost[i];
        }
        printf("%lld %lld\n",sumT,sumC);
        rep(i,0,N)
            adj[i].clear();
    }

    re 0;
}
void Dijkstra(){
    rep(i,0,N+1){
        dis[i]=INF;
        vis[i]=false;
    }
    dis[0]=0;

    Q.push((node){0,0});
    while(!Q.empty()){
        node now=Q.top();
        Q.pop();
        int minNode=now.index;
        vis[minNode]=true;

        rep(i,0,getLen(adj,minNode)){
            Edge &e=adj[minNode][i];
            int to=e.finish;
            int d=e.d;
            int c=e.c;
            if(!vis[to] && dis[to]>=dis[minNode]+d){
                if(dis[to]>dis[minNode]+d){
                    dis[to]=dis[minNode]+d;
                    cost[to]=c;
                    Q.push((node){to,dis[to]});
                }else{
                    if(dis[to]==dis[minNode]+d)
                        cost[to]=min(cost[to],c);
                }
            }
        }

    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值