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\n", d, c);
    }
    return 0;
}
时间: 2024-10-23 05:36:30

zoj3946--Highway Project的相关文章

ZOJ-3946 Highway Project (最短路)

题目大意:一张带权无向图,权有两个参数(d,c),分别表示走过这条边的时间和建造这条边的代价.要求选出一些边,使得0节点到其他点的距离之和最短,并在最短的基础上求最小代价. 题目分析:这是16年浙江省赛的一道题.先求出0到所有点的最短路,然后找出所有可能在最短路径上的边,最后在每一个节点的入边之中都选一条具有最小代价的边.多么简单的一道题!!! 代码如下: # include<iostream> # include<cstdio> # include<cstring>

ZOJ 3946 Highway Project 贪心+最短路

题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存在一系列边(ui,v)使得dis[v]最小(dis[v]表示0到v的距离).这些边能且只能选一条,那么我们自然应该选cost最小的那个边了. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #inc

zoj 3946 Highway Project spfa

题意:一个帝国有 n 个城市,可以在城市两两之间建立 m 条高速公路,建立 x-y 之间的高速路需要时间 d,花费为 c, 最后建立完边(<=m)后使得首都 0 号城市到各个城市(1~n-1)的总时间最少,在多个时间满足条件下再选花费最少的. 思路:直接spfa,当有多个点使得时间最少时,选花费最小的边. #include <iostream> #include <algorithm> #include <string.h> #include <stdio.

ZOJ 3946 Highway Project

迪杰斯特拉最小堆 #include<cstdio> #include<cstring> #include<cmath> #include<map> #include<queue> #include<algorithm> using namespace std; const long long INF=9999999999999; const int maxn=2e5+10; struct X{ int id; long long ti

2016.4.23 浙江省赛题解

Apples and Ideas Time Limit: 2 Seconds      Memory Limit: 65536 KB "If you have an apple and I have an apple and we exchange these apples then you and I will still each have one apple. But if you have an idea and I have an idea and we exchange these

2016.4.23浙江省赛

A      Apples and Ideas Time Limit: 2 Seconds      Memory Limit: 65536 KB "If you have an apple and I have an apple and we exchange these apples then you and I will still each have one apple. But if you have an idea and I have an idea and we exchange

Team Foundation Server 2013 with Update 3 Install LOG

[Info   @10:14:58.155] ====================================================================[Info   @10:14:58.163] Team Foundation Server Administration Log[Info   @10:14:58.175] Version  : 12.0.30723.0[Info   @10:14:58.175] DateTime : 10/03/2014 18:1

(转载)解决AndroidStudio导入项目在 Building gradle project info 一直卡住

源地址http://blog.csdn.net/yyh352091626/article/details/51490976 Android Studio导入项目的时候,一直卡在Building gradle project info这一步,主要原因还是因为被墙的结果.gradle官网虽然可以访问,但是速度连蜗牛都赶不上... 解决办法主要有两种,一是直接下载gradle离线包,二是修改项目的gradle-wrapper.properties里的gradle版本为自己电脑已有的版本. 离线包下载导

maven -- 问题解决(三)Java compiler level does not match the version of the installed Java project facet

问题: Java compiler level does not match the version of the installed Java project facet 解决方法如下: properties->Java Compiler,修改JDK版本,然后Apply