ZOJ Problem Set - 3946
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 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 cityi (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).

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 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3946


题意:求最短时间和最少花费。先满足最短时间,再满足最少花费。

思路:n,m很大,要用SPFA算法,不能用邻接表,否则会爆内存。

代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
const int MAXN = 1e5+, mod = 1e9 + , inf = 0x3f3f3f3f;
typedef long long ll;
const ll INF = (1ll<<);
struct node
{
int to,d,c;
} edge[*MAXN];
int head[MAXN],next[*MAXN];
int sign[MAXN];
queue<int>Q;
ll dist[MAXN],cost[MAXN];
int n,m;
void add(int i,int u,int v,int d,int c)
{
edge[i].to=v;
edge[i].d=d;
edge[i].c=c;
next[i]=head[u];
head[u]=i;
}
void SPFA(int v)
{
int i,u;
for(i=; i<n; i++)
{
dist[i]=INF;cost[i]=INF;
sign[i]=;
}
dist[v]=;
cost[]=;
Q.push(v);
sign[v]=;
while(!Q.empty())
{
u=Q.front();
Q.pop();
sign[u]=;
i=head[u];
while(i!=-)
{
if(dist[edge[i].to]>dist[u]+edge[i].d)
{
dist[edge[i].to]=dist[u]+edge[i].d;
cost[edge[i].to]=edge[i].c;
if(!sign[edge[i].to])
{
Q.push(edge[i].to);
sign[edge[i].to]=;
}
}
else if(dist[edge[i].to]==dist[u]+edge[i].d)
{
if(cost[edge[i].to]>edge[i].c)
cost[edge[i].to]=edge[i].c;
}
i=next[i];
}
}
}
int main()
{
int i,j,T;
int x,y,d,c;
cin>>T;
while(T--)
{
cin>>n>>m;
memset(edge,,sizeof(edge));
memset(next,,sizeof(next));
for(i=; i<=n; i++) head[i]=-;
j=;
for(i=; i<=m; i++)
{
scanf("%d%d%d%d",&x,&y,&d,&c);
add(j,x,y,d,c);j++;
add(j,y,x,d,c);j++;
}
SPFA();
ll ans1=,ans2=;
for(i=; i<n; i++)
{
if(dist[i]!=INF) ans1+=dist[i];
if(cost[i]!=INF) ans2+=cost[i];
}
cout<<ans1<<" "<<ans2<<endl;
}
return ;
}

SPFA

 

ZOJ 3946.Highway Project(The 13th Zhejiang Provincial Collegiate Programming Contest.K) SPFA的更多相关文章

  1. The 13th Zhejiang Provincial Collegiate Programming Contest - D

    The Lucky Week Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward, the headmaster of the Marja ...

  2. The 13th Zhejiang Provincial Collegiate Programming Contest - I

    People Counting Time Limit: 2 Seconds      Memory Limit: 65536 KB In a BG (dinner gathering) for ZJU ...

  3. The 13th Zhejiang Provincial Collegiate Programming Contest - C

    Defuse the Bomb Time Limit: 2 Seconds      Memory Limit: 65536 KB The bomb is about to explode! Plea ...

  4. ZOJ 4033 CONTINUE...?(The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple)

    #include <iostream> #include <algorithm> using namespace std; ; int a[maxn]; int main(){ ...

  5. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Capture the Flag

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5503 The 12th Zhejiang Provincial ...

  6. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...

  7. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Beauty of Array

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5496 The 12th Zhejiang Provincial ...

  8. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Lunch Time

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5499 The 12th Zhejiang Provincial ...

  9. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Convert QWERTY to Dvorak

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5502  The 12th Zhejiang Provincial ...

随机推荐

  1. ubuntu16.04安装python3,numpy,pandas等量化计算库

    ubunt安装python3 sudo add-apt-repository ppa:fkrull/deadsnakessudo apt-get updatesudo apt-get install ...

  2. python 编码文件json.loads json.dumps

    import yaml d = {'name': '张三', 'age': '1'} print d jd = json.dumps(d, ensure_ascii=False, encoding=' ...

  3. mysql 解除安全模式

    问题:rror Code: 1175. You are using safe update mode and you tried to update a table without a WHERE t ...

  4. leetcode235

    /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...

  5. leetcode171

    public class Solution { private int ConvertToC(char c) { ; switch (c) { case 'A': case 'a': rnt = ; ...

  6. 前端-CSS-7-标准文档流&行内元素和块级元素转换

    1.什么是标准文档流 <!--  什么是标准文档流 宏观的将,我们的web页面和ps等设计软件有本质的区别 web 网页的制作 是个“流” 从上而下 ,像 “织毛衣” 而设计软件 ,想往哪里画东 ...

  7. python之类的相关知识

    面向对象技术简介 类: 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公用的.类变量定义在类中且在函数体之 ...

  8. locate包的安装

    linux中locate命令可以快速定位我们需要查找的文件,但是在yum中,locate的安装包名为 mlocate(yum list | grep locate可以查看),安装方法: yum -y ...

  9. Gson转换时,Double转式化

    package com.mall.core; import java.lang.reflect.Type; import java.text.DecimalFormat; import com.goo ...

  10. samtools软件的使用

    1)samtools简介--------------------------------------------------------------------------背景:前面我们讲过sam/b ...