首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >K - Highway Project  ZOJ - 3946 【 SPFA 求最小时间下最小距离】

K - Highway Project  ZOJ - 3946 【 SPFA 求最小时间下最小距离】

作者头像
Lokinli
发布2023-03-09 15:14:24
发布2023-03-09 15:14:24
3660
举报
文章被收录于专栏:以终为始以终为始

K - Highway Project

ZOJ - 3946 

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 Tindicating 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

代码语言:javascript
复制
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

代码语言:javascript
复制
4 3
4 4
代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f;
const ll maxn = 1e5 + 10;
struct node{
    ll to,w,p;  // w是时间、p是距离
    bool operator <(const node&b)const{
        if(w == b.w) return p > b.p;
        else return w > b.w;
    }
};
vector<node>ve[maxn];
ll dist[maxn],cost[maxn]; // dist 存的是时间,cost 是距离
ll n,m;
bool vis[maxn];
void spfa()  //这题的默认起点是 0 
{
    memset(dist,inf,sizeof(dist));
    memset(cost,inf,sizeof(cost));
    memset(vis,false,sizeof(vis));
    priority_queue<node>q;
    q.push((node){0,0,0});  // 将起点放进去
    dist[0] = 0; 
    cost[0] = 0; 
    while(!q.empty())
    {
        node top = q.top();
        q.pop();
        ll ww = top.w;
        ll pp = top.p;
        ll vv = top.to;
        if(vis[vv])continue; // 如果以前以这个点为转折松弛过,就不用再更新了
        vis[vv] = true; // 标记
        ll len = ve[vv].size();
        for(ll i = 0; i < len; i ++)
        {
            ll v = ve[vv][i].to;
            if(!vis[v])
            {
                ll w = ve[vv][i].w; ll p = ve[vv][i].p;
                if(dist[v] > w + ww || (dist[v] == w + ww && cost[v] > p))
                {
                    cost[v] = p;
                    dist[v] = w+ww;
                    q.push((node){v,dist[v],cost[v]});
                }
            }
        }
    }
}
int main()
{
    ll t,u,v,w,p;
    scanf("%lld", &t);
    while(t--)
    {
        scanf("%lld %lld", &n, &m);
        for(int i = 0; i <= n; i ++)ve[i].clear();
        for(ll i = 0; i < m; i ++)
        {
            scanf("%lld%lld%lld%lld",&u,&v,&w,&p);
            ve[u].push_back((node){v,w,p});
            ve[v].push_back((node){u,w,p});
        }
        spfa();
        ll ans, res;
        ans = res = 0;
        for(ll i = 0; i < n; i ++)
        {
            ans += dist[i];
            res += cost[i];
        }
        printf("%lld %lld\n", ans,res);
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-04-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • K - Highway Project
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档