ZOJ 3946.Highway Project(The 13th Zhejiang Provincial Collegiate Programming Contest.K) SPFA
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 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 题目链接: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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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(){ ...
- 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 ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...
- 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 ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Lunch Time
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5499 The 12th Zhejiang Provincial ...
- 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 ...
随机推荐
- jshint在bat批处理中闪退,代码中无法调用的问题
先说解决办法:加个call eg: call jshint --version Pause 具体原因有空再更
- lambda,sorted(),filter(),map(),递归,二分法
1. lambda 匿名函数 语法: lambda 参数:返回值 不能完成复杂的操作例 # li=['21','asdd','weqeqw','wqf']# # it=iter(li)# # prin ...
- python内置函数详细介绍
知识内容: 1.python内置函数简介 2.python内置函数详细介绍 一.python内置函数简介 python中有很多内置函数,实现了一些基本功能,内置函数的官方介绍文档: https: ...
- 20.Scrapy日常练手
1.创建爬虫项目: scrapy startproject tutorial 2.创建 spider cd tutorial scrapy genspider quotes quotes.tosc ...
- mysql 忽略某个错误 继续执行
执行如下存储过程: CREATE PROCEDURE `aa`()BEGINcall RealtimeData_9035();call RealtimeData_9504();call Realti ...
- TTreeView.OnCustomDrawItem
TTreeNode *node; node = , "AAAA"); TreeView1->Items->AddChild(node, "aaa1" ...
- VBA 连接文本的自定义函数(可用于数组公式)
Function ConTxt(ParamArray args() As Variant) As VariantDim tmptext As Variant, i As Variant, cellv ...
- javascript时间日期操作
Js获取当前日期时间及其它操作 var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); ...
- Enum学习中的compareTo方法分析
今天看工厂模式的时候里面用了枚举定义各种可能的实例类型,就看了一下枚举,发现里面有一个compareTo(E o)方法 通过Object的getClass()方法比较两个两个比校对象类型是否一致,如果 ...
- sysbench——服务器cpu性能测试
一.前言 最近在工作中需要测试cpu占用率.内存占用率,我想要寻找一种合适的能提高cpu占用率的工具及方法.先尝试了使用 echo "scale=5000; 4*a(1)" | b ...