• zoj3946--Highway Project


    Highway Project

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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 ≤ iN). 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
    

    Author: Lu, Yi
    Source: The 13th Zhejiang Provincial Collegiate Programming Contest

    #include <cstdio>
    #include <queue>
    #include <cstring>
    #define N 110000*10
    typedef long long LL;
    
    using namespace std;
    
    LL tt[N], cost[N];
    const LL INF= 0x3f3f3f3f;
    int n, m;
    struct Edge
    {
        int v, next;
        LL t, c;
        Edge()
        {
        }
        Edge(int v, int next, LL t, LL c): v(v), next(next), t(t), c(c){
        } 
        
    }p[N];
    int cnt, vis[N], head[N];
    void addEdge(int u, int v, LL t, LL c)
    {
        /*p[cnt].v = v;  
        p[cnt].t = t;  
        p[cnt].c = c;  
        p[cnt].next = head[u];  
        head[u] = cnt++; */
        p[cnt]=Edge(v, head[u], t, c);
        head[u]= cnt++;
    }
    void spfa()
    {
        memset(vis, 0, sizeof(vis));
        memset(tt, INF, sizeof(tt));    
        memset(cost, INF, sizeof(cost));
        queue<int> q;
        q.push(0);
        vis[0]=1;
        cost[0]= tt[0]= 0;
        while(!q.empty())
        {
            int out= q.front(); q.pop();
            vis[out]= 0;
            for(int i=head[out]; i+1; i= p[i].next) 
            {
                if(tt[out]+p[i].t <= tt[p[i].v])
                {
                    if(tt[out]+p[i].t < tt[p[i].v])
                    {
                        tt[p[i].v]= tt[out]+ p[i].t;
                        cost[p[i].v]= p[i].c;
                        if(!vis[p[i].v])
                        {
                            q.push(p[i].v);
                            vis[p[i].v]= 1;
                        }
                    }
                    else if(cost[p[i].v]>p[i].c) //时间相同考虑费用小的边  
                    {
                        if(!vis[p[i].v])
                        {
                            q.push(p[i].v);
                            vis[p[i].v]=1;
                        }
                        cost[p[i].v]= p[i].c;
                    }
                }
            
            }
        }
    }
    
    int main()
    {
        int Q; 
        scanf("%d", &Q);
        while(Q--)
        {
            memset(head, -1, sizeof(head));
            cnt= 0; 
            scanf("%d%d", &n, &m);
            for(int i=0; i< m; i++)
            {
                int a, b, c, t;
                scanf("%d%d%d%d", &a, &b, &c, &t);
                addEdge(a, b, c, t);
                addEdge(b, a, c, t); 
            }
            spfa();
            LL d=0, c=0;
            for(int i=0; i< n; i++)
            {
                d += tt[i];
                c += cost[i];
            }
            printf("%lld %lld
    ", d, c);
        }
        return 0;
    }
  • 相关阅读:
    WPF技巧:通过代码片段管理器编写自己常用的代码模板提示效率
    C# 8.0和.NET Core 3.0高级编程 分享笔记二:编程基础第二部分
    C# 8.0和.NET Core 3.0高级编程 分享笔记二:编程基础第一部分
    WPF教程十一:简单了解并使用控件模板
    C# 8.0和.NET Core 3.0高级编程 分享笔记一:C#8.0与NET Core 3.0入门
    WPF教程十:如何使用Style和Behavior在WPF中规范视觉样式
    概率论与数理统计图式(第一章 概率论的基本概念)1.2概率
    概率论与数理统计图式(第一章 概率论的基本概念)1.1随机事件与随机变量
    软件工程基础图式(第三章 需求分析)
    软件工程基础图式(第二章)
  • 原文地址:https://www.cnblogs.com/soTired/p/5433080.html
Copyright © 2020-2023  润新知