【ZOJ - 3946】Highway Project(最短路子图,维护双权值,贪心,最小树形图)

题干:

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 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 Tindicating 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

题目大意:

给你一个无向图,每条边有一个距离c和花费d,叫你选一些边,使得点0到其他所有点的距离之和最小,其次,使总花费最小。

解题报告:

   因为总题最优解一定满足局部局部解,所以距离和最小就可以用求最短路来求解,最后统计个答案就可以。对于第二个权值的维护,可以在Dijkstra的同时贪心第二个权值,也可以先构造一个最短路子图,求其中的最短路树,也就是求最小树形图。(注意这里不是Kruskal可以解决的,,,因为其实这个新图加的是有向边、、、)

比如这个样例:

不难看出,最短路子图应该是这样的:

而没有由上到下的那条边。

 

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
struct Edge {int fr,to;ll w,c;int ne;
} e[MAX];
struct Graph {struct Point {int pos;ll c;Point() {}Point(int pos,ll c):pos(pos),c(c) {}bool operator<(const Point b) const {return c > b.c;}};Edge e[MAX];int tot = 0;//总共tot条边,编号0~(tot-1)int head[MAX];bool vis[MAX];ll d[MAX],cost[MAX];void add(int u,int v,ll w,ll c) {e[tot].fr = u;e[tot].to = v;e[tot].ne = head[u];e[tot].w = w;e[tot].c = c;head[u] = tot++;}void Dijkstra(int st) {priority_queue<Point> pq;memset(d,0x3f,sizeof d);memset(cost,0x3f,sizeof cost);memset(vis,0,sizeof vis);d[st] = 0;cost[st] = 0;pq.push(Point(st,0));while(pq.size()) {Point cur = pq.top();pq.pop();if(vis[cur.pos]) continue;vis[cur.pos] = 1;for(int i = head[cur.pos]; ~i; i = e[i].ne) {if(d[e[i].to] > d[cur.pos] + e[i].w) {d[e[i].to] = d[cur.pos] + e[i].w;cost[e[i].to] = e[i].c;pq.push(Point(e[i].to,d[e[i].to]));}else if(d[e[i].to] == d[cur.pos]+e[i].w) {if(cost[e[i].to] > e[i].c) {cost[e[i].to] = e[i].c;pq.push(Point(e[i].to,d[e[i].to]));}}}}}void id_add(int u,int v,ll w,ll c) {e[tot].fr = u;e[tot].to = v;e[tot].ne = head[u];e[tot].w = w;e[tot].c = c;head[u] = tot++;}void init(int n) {tot = 0;for(int i = 0; i<=n; i++) head[i] = -1;}
} G1;
int main() {int n,m;int u,v;ll w,c;int t;cin>>t;while(t--) {scanf("%d%d",&n,&m);G1.init(n);for(int i = 1; i<=m; i++) {scanf("%d%d%lld%lld",&u,&v,&w,&c);u++,v++;G1.add(u,v,w,c);G1.add(v,u,w,c); }G1.Dijkstra(1);ll ans1 = 0,ans2 = 0;for(int i = 1; i<=n; i++) ans1 += G1.d[i],ans2 += G1.cost[i];printf("%lld %lld\n",ans1,ans2);}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440442.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【ZOJ - 3956】Course Selection System(01背包)

题干&#xff1a; There are n courses in the course selection system of Marjar University. The i-th course is described by two values: happiness Hi and credit Ci. If a student selects m courses x1, x2, ..., xm, then his comfort level of the semester can be…

Linux把文件移动到容器外,Docker容器与主机之间拷贝文件的方法

一般情况下&#xff0c;我们在启动Docker容器的时候可以使用-v参数映射宿主机的文件或者目录到容器里&#xff0c;这样的话&#xff0c;在宿主机相关目录下的文件修改会自动在容器里生效。但是&#xff0c;如果我们已经启动了一个容器的话&#xff0c;就只能使用下面的这种方式…

【计蒜客 - 2019南昌邀请赛网络赛 - H】Coloring Game(找规律,思维dp)

题干&#xff1a; David has a white board with 2 \times N2N grids.He decides to paint some grids black with his brush.He always starts at the top left corner and ends at the bottom right corner, where grids should be black ultimately. Each time he can mov…

【HDU - 6514】Monitor(二维差分,前缀和)

题干&#xff1a; Monitor Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 163840/163840 K (Java/Others) Total Submission(s): 872 Accepted Submission(s): 145 Problem Description Xiaoteng has a large area of land for growing crops, and the land…

【CodeForces - 1153D】Serval and Rooted Tree(树形dp)

题干&#xff1a; Now Serval is a junior high school student in Japari Middle School, and he is still thrilled on math as before. As a talented boy in mathematics, he likes to play with numbers. This time, he wants to play with numbers on a rooted tree. …

实验楼Linux基础挑战2答案,实验楼-Linux基础-实验二 Linux的基本概念及操作

一、实验介绍1.1 实验内容实验楼环境介绍常用 Shell 命令及快捷键Linux 使用小技巧1.2 实验知识点Linux 基本命令通配符的使用查看帮助文档二、桌面环境1.Linux 桌面环境介绍相对于现在的 Windows 系统&#xff0c;UNIX/Linux 本身是没有图形界面的&#xff0c;我们通常在 UNIX…

【HDU - 1533】Going Home(网络流,二分图最优匹配,KM算法)

题干&#xff1a; On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step h…

【ZOJ - 4029】Now Loading!!!(整除分块,思维,二分,前缀和)

题干&#xff1a; 其中 zi 是第i次询问后的z。 解题报告&#xff1a; 因为有取log运算&#xff0c;所以分母的取值肯定不会超过30种&#xff0c;所以分每一个分母的时候&#xff0c;用前缀和优化一个和&#xff0c;最后求乘积就行了。&#xff08;其实不需要快速幂&#xff0c…

【ZOJ - 4032】Magic Points (思维,几何,构造)

题干&#xff1a; 解题报告&#xff1a; 想到了&#xff0c;这样绕圈构造。但是这样有个问题&#xff0c;最后一个点如何构造。 刚开始想的是n奇数 &#xff0c; 就8 10 这样的连一条&#xff0c;n偶数 就8 11 这样的连一条&#xff0c;随便构造一下就行&#xff0c;但是发…

android 仿真翻页动画,Android 两Activity之间动画效果(1)---------翻页效果

用Android rotate动画实现翻页效果&#xff0c;效果如图&#xff1a;要实现上面动画&#xff0c;首先搞明白rotate动画原理&#xff1b;(1)Degrees坐标&#xff1a;0度(360度)270度90度 顺时针旋转 180(2)rotate 关键属性fromDegrees 开始旋转时角度 toDegrees 结束时的角…

android 存储不被垃圾清理,手机内存足够大,就不需要清理垃圾了?你错了!

原标题&#xff1a;手机内存足够大,就不需要清理垃圾了?你错了!中新网4月20日电今天,人们使用智能手机的时间已超过电脑,希望在任何时候、任何地方,一部手机搞定所有。对手机的流畅度、性能和安全的要求越来越高。新手机刚到手时非常流畅,用一段时间就出现各种卡顿,网民对猎豹…

android和ios系统的内存,WP和Saipan系统的流畅程度相当于ios,占用的内存很少,但是为什么要用Android取代它...

当涉及到WP和Symbian系统时&#xff0c;许可能没有听说过它&#xff0c;但是对于大多数关注智能手机市场增长的消费者来说&#xff0c;它已经为人们所熟悉&#xff0c;并且许已经使用了它. 当时在功能性机器上使用了Saipan系统&#xff0c;但是您会发现该系统的流畅性与当时的i…

android+微信一键关注,一键关注微信公众平台JS代码有哪些?

一键关注微信公众平台JS代码有哪些&#xff1f;在网页设置一个按钮或者链接可以让用户一键关注微信公众平台&#xff0c;那么这种一键关注微信公众平台的功能如何实现呢&#xff1f;下面小编分享给大家一键关注微信公众平台的JS代码。在微信上&#xff0c;通过微信公众平台推送…

asp.net 写入html代码,asp.net读取模版并写入文本文件

本文要介绍的是ASP.NET怎样读写文本文件&#xff0c;但更重要的是实现的过程。使用的工具是Visual Studio 2015 &#xff0c;.NET版本是4.6.1 。一共建立的2个项目&#xff0c;HoverTreePanel和HoverTreeWeb&#xff0c;都是ASP.NET项目。文章末尾附源码下载。项目结果如下图&a…

html以图像中心定位,在HTML图像上水平和垂直居中文本(绝对定位)

Michael Roach0htmlcssflexbox鉴于以下设计元素,我试图在html中包含图像,以便可以使用css过渡(悬停效果)操纵不透明度.这里的主要缺点是我使用手动垂直居中(绝对/顶部:4​​0%),这在缩小浏览器时变得明显.在使用绝对定位时,是否可以使用flexbox或table进行垂直居中&#xff1f;…

*【CodeForces - 1150D】Three Religions(dp,预处理,思维)

题干&#xff1a; During the archaeological research in the Middle East you found the traces of three ancient religions: First religion, Second religion and Third religion. You compiled the information on the evolution of each of these beliefs, and you now…

华人科学家量子计算机,华人科学家在美国研发出性能强大的光子计算机,能够与中国的量子计算机一战高下!...

原标题&#xff1a;华人科学家在美国研发出性能强大的光子计算机&#xff0c;能够与中国的量子计算机一战高下&#xff01;在最近的《自然纳米技术》杂志上&#xff0c;一篇来自美国哥伦比亚大学的论文在业界掀起了轩然大波&#xff0c;一位名叫虞南方的物理学助理教授成功率领…

【BZOJ - 1001】狼抓兔子(无向图网络流,最小割,或平面图转对偶图求最短路SPFA)

题干&#xff1a; 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到&#xff0c;但抓兔子还是比较在行的&#xff0c; 而且现在的兔子还比较笨&#xff0c;它们只有两个窝&#xff0c;现在你做为狼王&#xff0c;面对下面这样一个网格的地形&#xff1a; …

1.UNIX网络编程卷1:源码配置

本节主要介绍UNIX网络编程卷1&#xff08;第三版&#xff09;在Ubuntu16.04的配置问题&#xff0c;并运行一个简单时间获取客户程序。 1.首先下载源文件&#xff0c;链接如下&#xff1a;UNIX Network Programming Source Code 2.将下载好的压缩文件unpv13e.tar.gz解压&#…

html 地球大气,地球大气层为什么永远不会消失?

地球的大气层是一个很神奇的存在&#xff0c;几十亿年来&#xff0c;它就如同一层厚厚的保护膜&#xff0c;将地球与太空完全阻隔起来&#xff0c;正因为如此&#xff0c;地球上的生命才能够繁衍生息&#xff0c;代代相传。相信很多人都会有这样的疑问&#xff0c;为什么地球上…