ZOJ 3946 Highway Project (spfa)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718

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


0~n-1个城市,然后可以修M条路。走完每条路要Di的时间,修这条路要Ci的花费。
然后让你修一些路使其满足从0到其他每个城市的总的时间花费最短。在这个条件满足的情况下下,找总的最小的花费。


解法: 依题意得,求每个点到0这个点的最短路径,明显spfa。
利用贪心的思想,取每个点的入边的最小花费,相加可得答案。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

const int M = 1e5 + 100;
const long long INF = 0x3f3f3f3f;
typedef long long ll;
struct Node{
    int next,to;
    ll d,c;
    Node(){}
    Node(int a,int b,ll d,ll c):next(a),to(b),d(d),c(c){}
}Edge[M*2];
int head[M]; int nedges; ll dis[M];
ll in[M],vis[M];
void add_edge(int a,int b,ll c,ll d)
{
    Edge[++nedges] = Node(head[a],b,c,d);
    head[a] = nedges;
    Edge[++nedges] = Node(head[b],a,c,d);
    head[b] = nedges;
}
void spfa(int n)
{
    queue<int>que;
    dis[0] = 0;
    que.push(0);
    vis[0] = true;
    while(!que.empty())
    {
        int now = que.front();
        que.pop();
        vis[now] = false;

        for(int i = head[now];~i;i=Edge[i].next)
        {
            int v = Edge[i].to;
            if(dis[now] + Edge[i].d <= dis[v])
            {
                if(dis[now] + Edge[i].d == dis[v])
                   in[v] = min(in[v],Edge[i].c);
                else in[v] = Edge[i].c;
                dis[v] = dis[now] + Edge[i].d;
                if(!vis[v])
                {
                    que.push(v);
                    vis[v] = true;
                }
            }
        }
    }
    ll nowl = 0,nowr = 0;
    for(int i=1;i<n;i++) nowl += dis[i],nowr += in[i];
    printf("%lld %lld\n",nowl,nowr);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        nedges = -1;
        memset(head,-1,sizeof(head));
        memset(dis,INF,sizeof(dis));
        memset(vis,false,sizeof(vis));
        memset(in,INF,sizeof(in));
        scanf("%d%d",&n,&m);
        while(m--)
        {
            int x,y;
            ll d,c;
            scanf("%d%d %lld%lld",&x,&y,&d,&c);
            add_edge(x,y,d,c);
        }
        spfa(n);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值