[Dijkstra] Highway Project 16浙江省赛K

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 NM (1 ≤ NM ≤ 105).

Then followed by M lines, each line contains four integers XiYiDiCi (0 ≤ XiYi < N, 0 < DiCi < 105).

<h4< dd="">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.

<h4< dd="">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

一道非常标准的dijkstra算法题,使用邻接表优化,注意好距离的储存需要会爆Int 要用Long long(否则在我写的dijkstra()函数中爆了int,会导致距离为负数,从而走不出循环,TLE) 就没有什么难度了,代码注释了蛮多,下面贴代码;

#include<bits/stdc++.h>
using namespace std;
const int MAXN=200000+10;
long long inf;
int first[MAXN],next[MAXN],u[MAXN],v[MAXN],w[MAXN],t[MAXN],n,m;//数组模拟邻接表 
long long dis[MAXN][3]; //距离储存要long long  否则会爆 
typedef pair<long long ,int>pii;  //以距离为第一判断要素,第二个为编号 
void init()
{
memset(first,-1,sizeof(first));  //邻接表初始化 
cin>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>u[i]>>v[i]>>w[i]>>t[i];
next[i]=first[u[i]];first[u[i]]=i;
u[i+m]=v[i];v[i+m]=u[i];w[i+m]=w[i];t[i+m]=t[i];   //无向图 
next[i+m]=first[u[i+m]];first[u[i+m]]=i+m;
}
for(int i=1;i<n;i++)
{
dis[i][1]=inf;  //求最小值初始化成最大值 
dis[i][2]=inf;
}
}
void dijkstra()
{
priority_queue<pii,vector<pii>,greater<pii> >que;  //由小到大进行优先队列储存 
que.push(pii(0,0));  //“0”点为起始点,距离为0。 
while(!que.empty())
{
pii temp=que.top();
que.pop();
int x=temp.second;
if(dis[x][1]!=temp.first)  //如果距离值不同,说明已经经过处理 
continue;
for(int e=first[x];e!=-1;e=next[e])
{
if(dis[v[e]][1]>dis[x][1]+w[e])
{
dis[v[e]][1]=dis[x][1]+w[e];
dis[v[e]][2]=t[e];
que.push(pii(dis[v[e]][1],v[e]));
}
else if(dis[v[e]][1]==dis[x][1]+w[e]&&dis[v[e]][2]>t[e])  //当第一要素相同,考虑第二要素 
dis[v[e]][2]=t[e];   
}
}
}
int main()
{
int T;
cin>>T;
while(T--)
{
inf=1e10;   //初始化最大值 
init();
dijkstra();
long long m1=0,m2=0;  //long long储存 
for(int i=1;i<n;i++)
{
m1+=dis[i][1];
m2+=dis[i][2];
}
cout<<m1<<" "<<m2<<endl;
}
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值