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 ...
随机推荐
- tensorboard-sklearn数据-loss
记录sklearn数据训练时的loss值,用tensorboard可视化 三步骤:红字处 import tensorflow as tf from sklearn.datasets import lo ...
- 21. oracle游标循环例子
事例1: create or replace procedure sp_addProjectQj( ret out number, flowid in number --流程Id) ascursor ...
- 4. java乱码处理
//返回到jsp页面 //request.setCharacterEncoding("utf-8"); //response.setContentType("text/h ...
- vs2015 调试IIS
vs2015 调试IIS vs2015,menu,调试>附加到进程>w3wp 然后用浏览器打开网页,单步调试跟踪. http://blog.csdn.net/hyperhawk/artic ...
- span width不起作用,border 无效
span属于内联元素,因此width对内联元素不起作用. 需要设置display:inline-block;使其成为内联级块级元素: border不起作用,主要是未设置border-style:sol ...
- 值得推荐的C/C++开源框架和库
值得推荐的C/C++开源框架和库 转自:http://www.cnblogs.com/lidabo/p/5514155.html - 1. Webbench Webbench是一个在Linux下 ...
- kernel TCP time wait bucket table overflow
# 故障描述 有一个需求是实时分析API接口访问日志,提取token去数据库查询对应的uid,然后收集一些指标存入到hbase中. 当程序执行一会后会被系统杀死 Killed ! # 故障排查 .CP ...
- 状态图(Statechart Diagram)
一.概念: 状态图用来描述一个特定对象的所有可能状态以及由于各种事件的发生而引起的状态之间的转移. 二.状态图的基本元素: 1.状态(State):指在对象的生命期中满足某些条件.执行某些活动或等待某 ...
- 回调(CallBack)
又名钩子函数(C语言里Hook) 不知道如何实现,可以写个回调, 相当于提供个钩子,让别人来挂东西,来实现. 其实就是用多态,实现了分离 . package cn.bjsxt.oop.callback ...
- 列表中语句 python
找到两个列表中相同元素: list1 = [1,2,3,4,5] list2 = [4,5,6,7,8] print ([l for l in list1 if l in list2]) 输出: [4 ...