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 Diminutes 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
Author: Lu, Yi
Source: The 13th Zhejiang Provincial Collegiate Programming Contest
题意:有n个点(编号0~n-1),m条边的一个图,每条边都有权值和搭建这条边所需要的花费。现在从0号点出发,要使得它到其余点的路径和最小,而且在路径和最小的情况下,要求搭建边的花费最少。
解题思路:首先考虑一个点到其余点最短,可以用dijkstra来解决。其次是搭建边的花费,最终构建的图肯定是一个生成树,所以每个点在最短路更新答案的时候只需要记录树连接当前点的那条边的花费即可。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;
#define LL long long
const LL INF=0x3f3f3f3f3f3f3f3f;
int n,m;
LL dis[100050],l[200050],a[200050],sum[100050];
int s[100050],nt[200050],e[200050],p[100050],visit[100050];
struct node
{
int id;
LL l;
friend bool operator <(node a,node b)
{
return a.l>b.l;
}
};
void Dijkstra()
{
memset(visit,0,sizeof visit);
for(int i=0;i<n;i++) dis[i]=INF,sum[i]=INF;
dis[0]=sum[0]=0;
node pre,nt1;
pre.id=0,pre.l=0;
priority_queue<node>q;
q.push(pre);
while(!q.empty())
{
pre=q.top();
q.pop();
visit[pre.id]=1;
for(int i=s[pre.id];~i;i=nt[i])
{
int ee=e[i];
if(visit[ee]) continue;
if(dis[ee]>dis[pre.id]+l[i])
{
dis[ee]=dis[pre.id]+l[i];
sum[ee]=a[i];
p[ee]=i;
nt1.id=ee;
nt1.l=dis[ee];
q.push(nt1);
}
else if(dis[ee]==dis[pre.id]+l[i]&&a[i]<sum[ee])
{
p[ee]=i;
sum[ee]=a[i];
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int u,v,tt,w,cnt=1;
memset(s,-1,sizeof s);
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
scanf("%d%d%d%d",&u,&v,&tt,&w);
nt[cnt]=s[u],s[u]=cnt,e[cnt]=v,l[cnt]=tt,a[cnt++]=w;
nt[cnt]=s[v],s[v]=cnt,e[cnt]=u,l[cnt]=tt,a[cnt++]=w;
}
Dijkstra();
LL ans1=0,ans2=0;
for(int i=0;i<n;i++) ans1+=dis[i];
for(int i=0;i<n;i++) ans2+=a[p[i]];
printf("%lld %lld\n",ans1,ans2);
}
return 0;
}