2016 浙江省赛 The 13th Zhejiang Provincial Collegiate Programming Contest K题 SPFA



链接:戳这里


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


思路:题目要求输出从首都到各个城市所需要的时间和的最小值

如果最小值有多个,那么输出建设道路话费最小的金额

我们先用spfa跑最短路,并在每次更新最短路的时候贪心去最小的道路花费


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,m,tot;
struct egde{
    int v,d,c,next;
}e[500100];
int head[100100],vis[100100];
ll dis[100100],cost[100100];
void init(){
    mst(head,-1);
    tot=0;
}
void Add(int u,int v,int d,int c){
    e[tot].v=v;
    e[tot].next=head[u];
    e[tot].d=d;
    e[tot].c=c;
    head[u]=tot++;
}
void Dijkstra(){
    for(int i=0;i<n;i++) dis[i]=INF;
    for(int i=0;i<n;i++) cost[i]=INF;
    for(int i=0;i<n;i++) vis[i]=0;
    queue<int> qu;
    qu.push(0);
    dis[0]=0;cost[0]=0;
    while(!qu.empty()){
        int u=qu.front();
        qu.pop();
        ///if(vis[u]) continue;
        vis[u]=0;
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].v;
            if(dis[v]==dis[u]+e[i].d){
                if(cost[v]>e[i].c){
                    cost[v]=e[i].c;
                }
            }
            if(dis[v]>dis[u]+e[i].d){
                dis[v]=dis[u]+e[i].d;
                cost[v]=e[i].c;
                if(!vis[v]){
                    qu.push(v);
                    vis[v]=1;
                }
            }
        }
    }
    ll D=0,C=0;
    for(int i=0;i<n;i++) {
        ///printf("%lld %lld\n",dis[i],cost[i]);
        D+=dis[i],C+=cost[i];
    }
    printf("%lld %lld\n",D,C);
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        init();
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++){
            int u,v,d,c;
            scanf("%d%d%d%d",&u,&v,&d,&c);
            Add(u,v,d,c);
            Add(v,u,d,c);
        }
        Dijkstra();
    }
    return 0;
}


你可能感兴趣的:(2016 浙江省赛 The 13th Zhejiang Provincial Collegiate Programming Contest K题 SPFA)