ZOJ 3946 Highway Project(Dijkstra)
来源:互联网 发布:matlab和python 编辑:程序博客网 时间:2024/06/05 15:00
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
24 50 3 1 10 1 1 10 2 10 102 1 1 12 3 1 24 50 3 1 10 1 1 10 2 10 102 1 2 12 3 1 2
Sample Output
4 34 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 100000struct 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 %lld\n",ans1,ans2); } return 0;}
- ZOJ 3946 Highway Project(Dijkstra)
- ZOJ 3946 Highway Project【dijkstra】【贪心】
- ZOJ 3946Highway Project
- zoj 3946 Highway Project
- ZOJ 3946 Highway Project
- ZOJ 3946 Highway Project
- ZOJ Highway Project(dijkstra&heap&邻接表)
- ZOJ 3946Highway Project【spfa】
- ZOJ 3946 Highway Project (spfa)
- Highway Project 浙江省第十三届省赛K题 ZOJ 3946(dijkstra+邻接表+优先队列)
- ZOJ 3946 Highway Project(最小费用最短路,优先队列优化dijkstra)
- ZOJ 3946 Highway Project 单源最短路
- ZOJ-3946-Highway Project(最短路)
- ZOJ Problem Set - 3946 Highway Project
- zoj 3946 Highway Project(最短路径)
- zoj--3946--Highway Project(SPFA)
- ZOJ 3946 Highway Project 优先队列dijk
- ZOJ Problem Set - 3946 Highway Project
- 3--编译预处理
- 栈的运用-四则运算表达式求值
- Anroid 加载图片方式一 使用SmartImageView
- 使用SQl方法把多行字符直接转换成相应的表输出
- 全端工程师必备技能汇总
- ZOJ 3946 Highway Project(Dijkstra)
- 优先级队列
- 官文:从今天开始开发iOS应用(Swift)第一章4~5节纯中文
- 训练深度神经网络的时候需要注意的技巧
- XDU-1138 Z1+Z2 (水~复数相加) From 西电校赛网络赛
- 庖丁解牛之仿《闪传》实现文件传输(中)
- "软件包 opera-stable 需要重新安装,但是我无法找到相应的安装文件 " 解决办法
- android Gradle的神奇之处
- BZOJ1338: Pku1981 Circle and Points单位圆覆盖