ZOJ 3946 Highway Project(Dijkstra)
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 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 city i (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
Dijkstra 用优先队列 ,用邻接表建立图,注意要long long int。比赛最后一分钟发现数组没有long long int. 还有在更新s数组的要加等号,#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <string>
#include <stdio.h>
#include <queue>
using namespace std;
const long long int INF=1000000000000000;
#define MAX 100000
struct Node
{
int value;
int next;
long long int time;
long long int cost;
}edge[MAX*2+5];
struct Node2
{
int time;
int cost;
int value;
Node2(){};
Node2(int time,int cost,int value)
{
this->time=time;
this->cost=cost;
this->value=value;
}
friend bool operator<(Node2 a,Node2 b)
{
if(a.time==b.time)
return a.cost>b.cost;
return a.time>b.time;
}
};
int cot;
int head[MAX+5];
int vis[MAX+5];
long long int s[MAX+5];
long long int ans1;
long long int ans2;
int n,m;
void add(int x,int y,long long int time,long long int cost)
{
edge[cot].value=y;
edge[cot].next=head[x];
edge[cot].time=time;
edge[cot].cost=cost;
head[x]=cot++;
}
void Dijkstra()
{
priority_queue<Node2> q;
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
s[i]=INF;
s[0]=0;
q.push(Node2(0,0,0));
while(!q.empty())
{
Node2 term=q.top();
q.pop();
if(vis[term.value]) continue;
vis[term.value]=1;
ans2+=term.cost;
int a=head[term.value];
while(a!=-1)
{
if(s[edge[a].value]>=s[term.value]+edge[a].time)
{
s[edge[a].value]=s[term.value]+edge[a].time;
q.push(Node2(s[edge[a].value],edge[a].cost,edge[a].value));
}
a=edge[a].next;
}
}
}
int main()
{
int t;
scanf("%d",&t);
int x,y;
long long int xx,yy;
while(t--)
{
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
cot=0;
for(int i=1;i<=m;i++)
{
scanf("%d%d%lld%lld",&x,&y,&xx,&yy);
add(x,y,xx,yy);
add(y,x,xx,yy);
}
ans1=0;ans2=0;
Dijkstra();
for(int i=1;i<n;i++)
ans1+=s[i];
printf("%lld %lldn",ans1,ans2);
}
return 0;
}
- Flash/Flex学习笔记(10):FMS 3.5之Hello World!
- Swarm基于多主机容器网络-overlay networks 梳理
- 未来AI可能会淘汰180万个工作岗位,你感到恐惧了吗
- css基础:把所有背景图都集成在一张图片上,减少图片服务器请求次数
- Docker可视化界面(Consul+Shipyard+Swarm+Service Discover)部署记录
- windows 2008上启用防火墙后sqlserver 2005经常出现连接超时的解决办法
- 重温delphi之控制台程序:Hello World!
- Docker集中化web界面管理平台-Shipyard部署记录
- "Goole项目托管"及"CodePlex发布开源项目"要点
- Docker网络解决方案-Calico部署记录
- c#中开发ActiveX的学习笔记
- Android新手之旅(4) 通过HTTP访问web
- Flash/Flex学习笔记(9):ActionScript3.0与Javascript的相互调用
- Flash/Flex学习笔记(8):ActionScript3.0中的面对对象
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 安装指定版本的docker服务
- 你学BFF和Serverless了吗
- 如何使用Java连接Kerberos的Phoenix
- docker swarm的常用操作
- 组件库源码中这些写法你掌握了吗?
- spark-2.4.0-hadoop2.7-安装部署 4.1. Spark安装4.2. 环境变量修改4.3. 配置修改4.4. 分发到其他机器4.5. 启动spark
- spark-2.4.0-hadoop2.7-高可用(HA)安装部署 5.1. Spark安装5.2. 环境变量修改5.3. 配置修改5.4. 分发到其他机器5.5.
- spark-2.4.0-hadoop2.7-简单操作 2.1. 相关截图
- Navicat Premium 12.0.24安装与激活(亲测已成功激活) 2.1. 下载激活文件2.2. 激活步骤准备工作2.3. 激活Navicat
- VMware实现iptables NAT及端口映射
- Saltstack_使用指南01_部署
- Saltstack_使用指南02_远程执行-验证 2.1. Master与哪些minion正常通信2.2. 查看master与指定minion通信是否正常
- Saltstack_使用指南03_配置管理
- Saltstack_使用指南04_数据系统-Grains 4.1. grains条目项信息4.2. grains全部信息4.3. 查询grains指定信息5.1. m
- 揭开spring初始化方法的神秘面纱