zoj 3946 Highway Project【SPFA多个性质的最优化】

来源:互联网 发布:姚明05-06赛季数据 编辑:程序博客网 时间:2024/06/06 00:17
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 34 4

Author: Lu, Yi
Source: The 13th Zhejiang Provincial Collegiate Programming Contest

Submit   Status


代码:(注意:用SPFA时边的数组开成边的个数的最大值的2倍

#include <stdio.h>#include <string.h>#include <algorithm>#include <queue>#define LL long long#define INF 0x3f3f3f3fusing namespace std;int n,m;struct Edge{LL from,to,val,time,next;}edge[200005];//双向的边,所以应该是边的总数乘以2!!!wa哭我了!!! LL head[100005];LL vis[100005];LL dis[100005];LL tm[100005];LL edgenum;void addEdge(LL x,LL y,LL z,LL t){Edge E={x,y,z,t,head[x]};edge[edgenum]=E;head[x]=edgenum++;}void SPFA(){queue<LL>q;q.push(0);vis[0]=1;//进队列的要进行标记 dis[0]=0;tm[0]=0;while(!q.empty()){LL u=q.front();q.pop();vis[u]=0;//出来的要取消标记! for(LL i=head[u];i!=-1;i=edge[i].next){LL v=edge[i].to;if(tm[v]>tm[u]+edge[i].time){tm[v]=tm[u]+edge[i].time;dis[v]=edge[i].val;//这一点只将v点前面的路的代价赋值给它就行了! if(!vis[v])//没进队列的将它放进队列,用于更新它周围的点 {vis[v]=1;q.push(v);}}else if(tm[v]==tm[u]+edge[i].time){if(dis[v]>edge[i].val){dis[v]=edge[i].val;//这一点只将v点前面的路的代价赋值给它就行了! }}}}}int main(){LL T;scanf("%lld",&T);while(T--){edgenum=0;memset(head,-1,sizeof(head));scanf("%d%d",&n,&m);for(LL i=0;i<m;i++){LL x,y,z,t;scanf("%lld%lld%lld%lld",&x,&y,&t,&z);addEdge(x,y,z,t);addEdge(y,x,z,t);}memset(vis,0,sizeof(vis));memset(dis,INF,sizeof(dis));memset(tm,INF,sizeof(tm));SPFA();LL s1=0,s2=0;for(int i=1;i<n;i++){s1+=tm[i];s2+=dis[i];}printf("%lld %lld\n",s1,s2);}return 0;} 


0 0