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. 〖Python〗-- Django的Form组件

    [Django的Form组件] Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 Form类的使 ...

  2. python之集合操作

    1.集合的增操作 add(item):增加至集合中 update(set):  若不存在,更新至集合中 2.集合的删操作(五种) pop():  随机删除并返回值 remove(item): 删除va ...

  3. c++官方文档-动态内存

    #include<iostream> #include <new> using namespace std; int main() { /** * 动态内存 * url: ht ...

  4. Laravel基础

    一.Laravel核心目录文件介绍 app:程序的核心代码和业务逻辑代码,其中的Http目录是我们业务逻辑的存放点 bootstrap:包含框架启动的和自动加载文件 config:包含所有程序中的配置 ...

  5. Firefox内存占用过高解决方法

    刚开始使用firefox火狐浏览器的时候,你会发现firefox占用内存大,CPU占用率高,打开网页停顿等问题,其实这些是因为firefox没有进行优化,默认设置是标准的设置的原因,解决方法如下: 一 ...

  6. 机器学习入门-集成算法(bagging, boosting, stacking)

    目的:为了让训练效果更好 bagging:是一种并行的算法,训练多个分类器,取最终结果的平均值 f(x) = 1/M∑fm(x) boosting: 是一种串行的算法,根据前一次的结果,进行加权来提高 ...

  7. 定时器 setInterval(‘function()’, 2000)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Star打印机数据解密

    通过串口调试工具 抓取到的16进制文本; 如下 然后打开我们的文档,查看命令数据内容. 详情请密我QQ:1161588342  说明加好友原因

  9. 更改maven下载jar的仓库为阿里云仓库

    修改settings.xml <!-- 配置本地maven的仓库 --> <localRepository>D:\file\path\maven\repository</ ...

  10. 通过http方式关闭和重启Jenkins

    Jenkins关闭和重启实现方式.   1.关闭Jenkins 只需要在访问jenkins服务器的网址url地址后加上exit.例如我jenkins的地址http://localhost:8080/, ...