13th浙江省赛K Highway Project

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

这是一道单源最短路的题目(虽然第一眼窝觉得是最小生成树,兴奋滴写完了kruskal…),最朴素的dijkstra算法会TLE,因为松弛操作的时候要遍历一遍;我们可以用优先队列优化,这样每次出来的就是权值最小的边(在此感谢大佬滔滔的模版);这道题目和一般的最短路不同的是,有两个排序依据(第一是时间time,其次是费用cost);
那么我们用俩vector,一个Edges存边的(id,time,cost),另外一个存某点所连接的边在Edges中位置,松弛操作的时候如果遇到time[p.id]+q.time == time[q,to] && q.cost < cost[q.to],这时候的费用低了,更新cost。

Attention:题·time的计算是需要叠加的,就是0->1,0->1->2,时间是到每个点的和,而cost是修一条路就增加一条路的费用对于已4修建的路不需要在加上去。

代码如下:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
#define MAXN 100010
int N,M;
struct Edge{
    int from,to,time,cost;
    Edge(int u,int v,int t,int w):from(u),to(v),time(t),cost(w){};
};
struct node{
    ll id,t,c;
    friend bool operator < (const node &a,const node &b) { if (a.t == b.t)
            return a.c > b.c;
        else
            return a.t > b.t;
    }
};
vector<Edge>E;
vector<int>G[MAXN];
ll dist[MAXN];//保存时间
ll cost[MAXN];//保存费用
bool vis[MAXN];//是否遍历
void add_edge(int u,int v,int t,int w){
    E.push_back(Edge(u,v,t,w));
    G[u].push_back(E.size()-1);
    E.push_back(Edge(v,u,t,w));//双向的路
    G[v].push_back(E.size()-1);//下标位置
};
void dijkstra(){
    priority_queue<node> que;
    memset(dist,INF,sizeof(dist));
    memset(cost,INF,sizeof(cost));
    dist[0]=0;cost[0]=0;//
    memset(vis,false,sizeof(vis));
    node e={0,0,0};
    que.push(e);
    while(!que.empty()){
        node p=que.top();
        que.pop();
        if(vis[p.id])
            continue;
        vis[p.id]=true;
        for(int i=0;i<G[p.id].size();i++){
            Edge q=E[G[p.id][i]];
            if(dist[q.to]>dist[p.id]+q.time){
                dist[q.to]=dist[p.id]+q.time;
                cost[q.to]=q.cost;
                node s={q.to,dist[q.to],cost[q.to]};
                que.push(s);
                vis[s.id]=false;
            }
            else if(dist[p.id]+q.time==dist[q.to]&&q.cost<cost[q.to]){
                cost[q.to]=q.cost;
                node s={q.to,dist[q.to],cost[q.to]};
                que.push(s);
                vis[s.id]=false;
            }
        }
    }
}

int main() {
    int T;
    cin >> T;
    while (T--) {
        ll tsum = 0, csum = 0;
        scanf("%d%d", &N, &M);
        for (int i = 0; i <= N; i++) {
            E.clear();
            G[i].clear();
        }
        for (int i = 0; i < M; i++) {
            int u, v, t, w;
            scanf("%d%d%d%d", &u, &v, &t, &w);
            add_edge(u, v, t, w);
        }
        dijkstra();
        for (int i = 1; i < N; i++) {
            tsum += dist[i];
            csum += cost[i];
        }
        printf("%lld %lld\n", tsum, csum);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值