ZOJ 3946 Highway Project (最短路)
题意:单源最短路,给你一些路,给你这些路的长度,给你修这些路的话费,求最短路和最小花费。
析:本质就是一个最短路,不过要维护两个值罢了,在维护花费时要维护的是该路要花多少,而不是总的路线花费。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Node{
int v, c, d;
}; vector<Node> G[maxn];
struct node{
int u; LL d, c;
node(){ }
node(int uu, LL dd, LL cc) : u(uu), d(dd), c(cc) { }
bool operator < (const node &p) const{
return d > p.d || (d == p.d && c > p.c);
}
}; LL d[maxn], c[maxn]; void solve(){
priority_queue<node> pq;
pq.push(node(0, 0, 0));
d[0] = c[0] = 0; while(!pq.empty()){
node U = pq.top(); pq.pop();
int u = U.u;
for(int i = 0; i < G[u].size(); ++i){
Node &V = G[u][i];
int v = V.v;
if(d[v] > d[u] + V.d){
d[v] = d[u] + V.d;
c[v] = V.c;
pq.push(node(v, d[v], c[v]));
}
else if(d[v] == d[u] + V.d && c[v] > V.c){
c[v] = V.c;
pq.push(node(v, d[v], c[v]));
}
}
}
LL ans1 = 0, ans2 = 0;
for(int i = 1; i < n; ++i){
ans1 += d[i];
ans2 += c[i];
} printf("%lld %lld\n", ans1, ans2);
} int main(){
int T; cin >> T;
while(T--){
scanf("%d %d", &n, &m);
for(int i = 0; i < n; ++i){
G[i].clear();
c[i] = d[i] = LNF;
}
for(int i = 0; i < m; ++i){
int x;
Node u;
scanf("%d %d %d %d", &x, &u.v, &u.d, &u.c);
G[x].push_back(u);
swap(x, u.v);
G[x].push_back(u);
}
solve();
}
return 0;
}
ZOJ 3946 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(The 13th Zhejiang Provincial Collegiate Programming Contest.K) SPFA
ZOJ Problem Set - 3946 Highway Project Time Limit: 2 Seconds Memory Limit: 65536 KB Edward, the ...
- ZOJ 3946 Highway Project(Dijkstra)
Highway Project Time Limit: 2 Seconds Memory Limit: 65536 KB Edward, the emperor of the Marjar ...
- ZOJ 3946 Highway Project 贪心+最短路
题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存 ...
- ZOJ 3946 Highway Project
1.迪杰斯特拉最小堆 #include<cstdio> #include<cstring> #include<cmath> #include<map> ...
- (spfa) Highway Project (zoj 3946 )
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718 Highway Project Time Limit: 2 Seco ...
- ZOJ3946:Highway Project(最短路变形)
本文转载自:http://www.javaxxz.com/thread-359442-1-1.html Edward, the emperor of the Marjar Empire, wants ...
- 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年浙江省 ...
随机推荐
- NetBeans字体设置
01.找到自己java字体目录.我的目录是[C:\Program Files\Java\jdk1.7.0_21\jre\lib] 02.复制fontconfig.properties.src, 重命名 ...
- java入门了解11
1.码表 (一)码表种类 ASCII:美国标准信息交换码,用一个字节的7位可以表示 ISO8859-1:拉丁码表.欧洲码表,用一个字节的8位表示,对ASCII没用到空间补充了自己特有的 GB2312: ...
- zookeeper 配置文件注释
tickTime=2000 initLimit=5 syncLimit=2 dataDir=/opt/shencl/zookeeper/data/data0 dataLogDir=/opt/shenc ...
- ConcurrentHashMap以及HashMap,HashTable的区别
ConcurrentHashMap与HashMap,和HashTable 的区别? ConcurrentHashMap是一个线程安全的key-value数据结构,而HashMap不是.Concurre ...
- C中malloc的使用(转)
malloc函数 原型:extern void *malloc(unsigned int num_bytes); 用法:#include <malloc.h> 功能:分配长度为num_b ...
- 如何在node.js中使用neo4j
本章中你将会学到如何在node.js中使用neo4j图形数据库. 当你想存储或者查询和数据紧密关联的数据的时候,图形数据库很有用. neo4j是一个可有效存储,处理和查询你数据模型中紧密相连的元素的数 ...
- Jmete基础使用
1,jmeter下载与安装 Jmeter的运行需要JDK支持,所以需要先安装好jdk,并配置好环境变量: 下载地址:http://jmeter.apache.org/download_jmeter.c ...
- ACM学习历程—HDU 5459 Jesus Is Here(递推)(2015沈阳网赛1010题)
Sample Input 9 5 6 7 8 113 1205 199312 199401 201314 Sample Output Case #1: 5 Case #2: 16 Case #3: 8 ...
- Unity Webplayer installation error- Unity Webplayer update finished, but installed..
https://forum.unity3d.com/threads/unity-webplayer-installation-error-unity-webplayer-update-finished ...
- [转]CSS遮罩——如何在CSS中使用遮罩
特别声明:此篇文章由D姐根据Christian Schaefer的英文文章原名<CSS Masks – How To Use Masking In CSS Now>进行翻译,整个译文带有我 ...