ZOJ3946:Highway Project(最短路变形)
本文转载自:http://www.javaxxz.com/thread-359442-1-1.html
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 C[sub]i[/sub] dollars. It takes D[sub]i[/sub] minutes to travel between city X[sub]i[/sub] and Y[sub]i[/sub] 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 ≤ 10[sup]5[/sup]).
Then followed by M lines, each line contains four integers X[sub]i[/sub], Y[sub]i[/sub], D[sub]i[/sub], C[sub]i[/sub] (0 ≤ X[sub]i[/sub], Y[sub]i[/sub] < N, 0 < D[sub]i[/sub], C[sub]i[/sub] < 10[sup]5[/sup]).<h4< dd="">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.<h4< dd="">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
复制代码
<h4< dd="">Sample Output
- 4 34 4
复制代码
题意:给一个图,每条边有个距离和花费,要求创建一个子图,满足0点到其余点的距离总和最小,且边的总花费最小。
思路:条件一,每个点贪心的都选最短路就是最优解,条件二,类似最小生成树的kruskal算法,在条件一的基础上更改下松弛条件即可。
拓展:CF545E,求距离总和最小,且总的边的长度最小,其实那题也是类似最短路基础上跑最小生成树。
- # include <iostream># include <cstdio># include <cstring># include <queue>using namespace std;const int maxn = 2e5+30;long long dis[maxn], cost[maxn], x, y;int vis[maxn], Next[maxn], cnt;struct node{int u, v, w, c, next;}edge[maxn];queue<int>q;int scan(){ int res=0,ch,flag=0; if((ch=getchar())=="-") flag=1; else if(ch>="0"&&ch<="9") res=ch-"0"; while((ch=getchar())>="0"&&ch<="9") res=res*10+ch-"0"; return flag?-res:res;}void out(long long x){ if(x/10) out(x/10); putchar("0"+x%10);}void add(int u, int v, int w, int c){ edge[cnt] = {u, v,w,c,Next[u]};; Next[u] = cnt++; edge[cnt] = {v, u,w,c,Next[v]}; Next[v] = cnt++;}void spfa(){ memset(dis, 0x3f, sizeof(dis)); memset(cost, 0x3f, sizeof(cost)); memset(vis, 0, sizeof(vis)); while(!q.empty()) q.pop(); q.push(1); dis[1] = cost[1] = 0; while(!q.empty()) { int u = q.front(); q.pop(); vis[u] = 0; for(int i=Next[u]; ~i; i=edge[i].next) { int v=edge[i].v, w=edge[i].w, c=edge[i].c; if(dis[v] > dis[u]+w) { dis[v] = dis[u]+w; cost[v] = c; if(!vis[v]) { vis[v] = 1; q.push(v); } } else if(dis[v] == dis[u]+w) { if(cost[v] > c) { cost[v] = c; if(!vis[v]) { vis[v] = 1; q.push(v); } } } } }}int main(){ int t, n, m, u, v, w, c; t=scan(); while(t--) { x = y = cnt = 0; memset(Next, -1, sizeof(Next)); n=scan();m=scan(); for(int i=1; i<=m; ++i) { u=scan();v=scan();w=scan();c=scan(); add(u+1,v+1,w,c); } spfa(); for(int i=2; i<=n; ++i) { x += dis[i]; y += cost[i]; } out(x); putchar(" "); out(y); puts(""); } return 0;}
复制代码
ZOJ3946:Highway Project(最短路变形)的更多相关文章
- zoj 3946 Highway Project(最短路 + 优先队列)
Highway Project Time Limit: 2 Seconds Memory Limit: 65536 KB Edward, the emperor of the Marjar ...
- ZOJ - 3946-Highway Project(最短路变形+优先队列优化)
Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can ...
- ZOJ-3946 Highway Project (最短路)
题目大意:一张带权无向图,权有两个参数(d,c),分别表示走过这条边的时间和建造这条边的代价.要求选出一些边,使得0节点到其他点的距离之和最短,并在最短的基础上求最小代价. 题目分析:这是16年浙江省 ...
- ZOJ 3946 Highway Project (最短路)
题意:单源最短路,给你一些路,给你这些路的长度,给你修这些路的话费,求最短路和最小花费. 析:本质就是一个最短路,不过要维护两个值罢了,在维护花费时要维护的是该路要花多少,而不是总的路线花费. 代码如 ...
- POJ-1797Heavy Transportation,最短路变形,用dijkstra稍加修改就可以了;
Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Description Background Hugo ...
- POJ-2253.Frogger.(求每条路径中最大值的最小值,最短路变形)
做到了这个题,感觉网上的博客是真的水,只有kuangbin大神一句话就点醒了我,所以我写这篇博客是为了让最短路的入门者尽快脱坑...... 本题思路:本题是最短路的变形,要求出最短路中的最大跳跃距离, ...
- POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...
- POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...
- (spfa) Highway Project (zoj 3946 )
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718 Highway Project Time Limit: 2 Seco ...
随机推荐
- Java采用内部构造器Builder模式进行对类进行构建
好处: 能保证重叠构造器模式的安全性: 能保证JAVABeans模式的可读性: package cn.lonecloud.builder; /** * 使用内部类构建器来对这个类进行构造 * @Tit ...
- 剑指offer第六天
29.最小的K个数 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4. 解法一: Partition思想 允许改变原始数组的情况, ...
- HDU - 4496 City 逆向并查集
思路:逆向并查集,逆向加入每一条边即可.在获取联通块数量的时候,直接判断新加入的边是否合并了两个集合,如果合并了说明联通块会减少一个,否则不变. AC代码 #include <cstdio> ...
- c# 委托(Func、Action)
以前自己写委托都用 delegate, 最近看组里的大佬们都用 Func , 以及 Action 来实现, 代码简洁了不少, 但是看得我晕晕乎乎. 花点时间研究一下,记录一下,以便后期的查阅. 1.F ...
- CEPH RGW集群和bucket的zone group 不一致导致的404异常解决 及 使用radosgw-admin metadata 命令设置bucket metadata 的方法
问题现象: 最近在研究zonegroup的配置操作,发现在配置zonegroup后修改了default zone,导致访问对象报404错误. 问题原因: rgw 日志 报异常'request fo ...
- 网络基础tcp/ip协议五
传输层的作用: ip层提供点到点的链接. 传输层提供端到端的链接. 传输层的协议: TCP: 传输控制协议可靠的,面向链接的协议,传输效率低. UDP: 用户数据报协议,不可靠,无连接的服务,传输效率 ...
- java 集合框架(十五)Deque
一.概述 Deque是Queue的子接口,我们知道Queue是一种队列形式,而Deque则是双向队列,它支持从两个端点方向检索和插入元素,因此Deque既可以支持LIFO形式也可以支持LIFO形式.D ...
- Navicat远程连接阿里云服务器的mysql
问题描述: 本机为win10,mysql安装在阿里云(Ubuntu系统)上,本机使用Navicat远程连接mysql,遇到一些坑,求助于阿里云,最终解决,特此记录一下! 安装mysql sudo ap ...
- Adobe RIA 开发工程师认证考试大纲
AdobeRIA 开发工程师认证考试大纲 考题数量:共90道题,考试通过正确率:60% 考试时间:120分钟 试题种类:单选题.多选题和判断题 1. Adobe RIA基础知识(2道题) ...
- 一款PHP环境整合工具—VertrigoServ介绍
Vertrigo简介 VertrigoServ 是一个Windows平台下的非常专业的.易于安装的免费网络开发环境,它集成了Apache, PHP, MySQL, SQLite, SQLiteMana ...