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

2023-03-09 15:14:24 浏览数 (2)

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 %lldn", ans,res);
    }
    return 0;
}

0 人点赞