zoj--3946--Highway Project(SPFA)

来源:互联网 发布:csgo淘宝买激活码 编辑:程序博客网 时间:2024/05/22 01:49
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 toN - 1 (the capital is 0) and there are M highways can be built. Building thei-th highway costs Ci dollars. It takes Di minutes to travel between cityXi 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 cityi (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

24 50 3 1 10 1 1 10 2 10 102 1 1 12 3 1 24 50 3 1 10 1 1 10 2 10 102 1 2 12 3 1 2

Sample Output

4 3

4 4

题意:给出n个城市还有m条路,每条路都有一定的花费,建成之后跑起来也需要一定的时间,0是首都,现在要求0到每一个城市的距离都最短,输出花费,并且求出在最短距离下的花费有多少

思路:只是一个很简单的SPFA而已,因为点有点多,map存不下,所以地杰斯特拉肯定是不可以的,做这个题的时候没想到怎么存放花费,其实就是以时间为主要条件,然后花费作为第二个条件,dis[i]表示到达城市i的最短距离,cost[i]表示到达i所需要的最少花费的边,因为求dis的时候已经把树建好了,所以cost不需要考虑建树的问题,直接求最小就行

数据有点大,最好用long long

#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;#define MAXN 200000+100#define INF 0x3f3f3f3f#define ll long long struct node{ll u,v,time,cost;ll next;}edge[MAXN*10];ll cnt,n,m,vis[MAXN],head[MAXN],dis[MAXN],cost[MAXN];void add(ll u,ll v,ll time,ll len){node E={u,v,time,len,head[u]};edge[cnt]=E;head[u]=cnt++;}void SPFA(){memset(vis,0,sizeof(vis));memset(dis,INF,sizeof(dis));memset(cost,INF,sizeof(cost));queue<ll>q;vis[0]=1;dis[0]=0;q.push(0);while(!q.empty()){ll u=q.front();q.pop();vis[u]=0;for(ll i=head[u];i!=-1;i=edge[i].next){node E=edge[i];if(dis[E.v]>dis[E.u]+E.time){dis[E.v]=dis[E.u]+E.time;cost[E.v]=E.cost;if(!vis[E.v]){vis[E.v]=1;q.push(E.v);}}if(dis[E.v]==dis[E.u]+E.time&&cost[E.v]>E.cost){cost[E.v]=E.cost;}}}}int main(){ll t;scanf("%lld",&t);while(t--){scanf("%lld%lld",&n,&m);cnt=0;memset(head,-1,sizeof(head));ll u,v,time,len;for(ll i=0;i<m;i++){scanf("%lld%lld%lld%lld",&u,&v,&time,&len);add(u,v,time,len);add(v,u,time,len);}SPFA();long long sdis=0,scost=0;for(ll i=1;i<n;i++){sdis+=dis[i];scost+=cost[i];}printf("%lld %lld\n",sdis,scost);}return 0;}


0 0
原创粉丝点击