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年浙江省 ...
随机推荐
- Docker 单机网络
Docker Network相关命令 root@ubuntu:~# docker network --help Usage: docker network COMMAND Manage network ...
- 字典树 HDU 1075 What Are You Talking About
http://acm.hdu.edu.cn/showproblem.php?pid=1075 ;}
- 【JavaScript学习整理】js基础
HTML,CSS属于标记语言, JavaScript是基于客户端的脚本语言. 变量: 语法 var 变量名 = value var是系统内部关键字,用来声明变量 变量名规则: 1.不能以数字开头 ...
- 字节和unicode
一.字节和unicode Java内核是unicode的,就连class文件也是,但是很多媒体,包括文件/流的保存方式是使用字节流的.因此Java要对这些字节流经行转化. char是unicode的, ...
- jQuery学习(3)
可以在select中设置size属性的属性值,从而让下拉列表中的选项都显示出来. <!DOCTYPE html> <html> <head> <title&g ...
- NULL 与空字符串
空字符串 '' 不占内存空间; NULL占一个字节的空间; 空字符串 的判断用 == <> NULL值用 is null ifnull();
- 带SoapHeader验证的WebServices
一般在项目中,制作的都是基于SOAP协议的webservices,其描述语言是WSDL.但是有时候在项目中,需要保证webservices的安全,需要对其进行进行验证,那么我们就要实现SoapHead ...
- 一个可以拖拽的div
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- AngularJS学习笔记(三) 单页面webApp和路由(ng-route)
就我现在的认识,路由($route)这个东西(也许可以加上$location)可以说是ng最重要的东西了.因为angular目前最重要的作用就是做单页面webApp,而路由这个东西是能做到页面跳转的关 ...
- Logiscope学习网址
Logiscope测试机理 http://blog.csdn.net/cmy673986/article/details/9163247 http://www.cnitblog.com/qiuya ...