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
思路:也算是最短路的模板题,就不过是在求最短路的过程中,如果遇到从0点出发到当前的点的时间出现相同时,比较之前的花费和现在的花费取最小的,还有答案可能爆int的,所以应该使用long long;
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
#define N int(1e5+10)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
typedef long long LL;
struct point
{
int x;
LL cost,time;
point(int _x,LL _c,LL _t):x(_x),time(_c),cost(_t){}
};
vector< vector<point> >g;
LL vis[N],d[N],cost[N];
void spfa()
{
memset(d,inf,sizeof(d));
memset(cost,inf,sizeof(cost));
memset(vis,0,sizeof(vis));
vis[0] = 1;
d[0] = 0;
cost[0] = 0;
queue<int>q;
q.push(0);
while(!q.empty())
{
int x = q.front(); q.pop();
vis[x] = 0;
for(int i = 0; i<g[x].size(); i++)
{
int v = g[x][i].x;
if(v == x) continue;
LL Time = g[x][i].time;
LL Cost = g[x][i].cost;
if(d[v] > d[x] + Time)
{
d[v] = d[x] + Time;
cost[v] = Cost;
if(!vis[v]){
q.push(v);
vis[v] = 1;
}
}
else if(d[v] == d[x] + Time)//如果最小时间出现相同,取最小的花费
{
cost[v] = min(cost[v],Cost);
}
}
}
}
int main()
{
#ifdef CDZSC_June
freopen("t.txt", "r", stdin);
int _time_jc = clock();
#endif
int t1,n,m,x,y;
LL sum1,sum2,t,c;
scanf("%d",&t1);
while(t1--)
{
sum1 = sum2 = 0;
g.clear();
scanf("%d%d",&n,&m);
g.resize(n+10);
for(int i = 0; i<m ; i++)
{
scanf("%d%d%lld%lld",&x,&y,&t,&c);
g[x].push_back(point(y,t,c));
g[y].push_back(point(x,t,c));//添加反向边
}
spfa();
for(int i = 0; i<n ; i++)
{
sum1 += d[i];
sum2 += cost[i];
}
printf("%lld %lld\n",sum1,sum2);
}
return 0;
}
本文介绍了一个高速公路项目的算法问题,目标是从首都出发通过建设双向高速公路达到其他城市,寻找总时间最短且在时间相同时成本最低的建设方案。采用SPFA算法进行最短路径计算,并考虑了当到达某城市的最短时间相同时选择花费最小的情况。
1527

被折叠的 条评论
为什么被折叠?



