题意:单源最短路,给你一些路,给你这些路的长度,给你修这些路的话费,求最短路和最小花费。

析:本质就是一个最短路,不过要维护两个值罢了,在维护花费时要维护的是该路要花多少,而不是总的路线花费。

代码如下:

#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 (最短路)的更多相关文章

  1. zoj 3946 Highway Project(最短路 + 优先队列)

    Highway Project Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward, the emperor of the Marjar ...

  2. 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 ...

  3. ZOJ 3946 Highway Project(Dijkstra)

    Highway Project Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward, the emperor of the Marjar ...

  4. ZOJ 3946 Highway Project 贪心+最短路

    题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存 ...

  5. ZOJ 3946 Highway Project

    1.迪杰斯特拉最小堆 #include<cstdio> #include<cstring> #include<cmath> #include<map> ...

  6. (spfa) Highway Project (zoj 3946 )

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718   Highway Project Time Limit: 2 Seco ...

  7. ZOJ3946:Highway Project(最短路变形)

    本文转载自:http://www.javaxxz.com/thread-359442-1-1.html Edward, the emperor of the Marjar Empire, wants ...

  8. ZOJ - 3946-Highway Project(最短路变形+优先队列优化)

    Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can ...

  9. ZOJ-3946 Highway Project (最短路)

    题目大意:一张带权无向图,权有两个参数(d,c),分别表示走过这条边的时间和建造这条边的代价.要求选出一些边,使得0节点到其他点的距离之和最短,并在最短的基础上求最小代价. 题目分析:这是16年浙江省 ...

随机推荐

  1. poj 1258 最小生成树 模板

    POJ 最小生成树模板 Kruskal算法 #include<iostream> #include<algorithm> #include<stdio.h> #in ...

  2. Shiro 集成 Web

    Web 集成 Shiro 的练习项目. Servlet + Shiro 项目结构 新建Maven项目,pom配置如下 <project xmlns="http://maven.apac ...

  3. C++ Const 使用总结,代码实例亲测

    1. 修饰普通变量 修饰变量语法 const TYPE value  <==> TYPE const value 两者等价, 变量不可修改,无需说明. 2. 修饰指针 首先看下面一段 代码 ...

  4. HashMap去重

    package util; import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import jav ...

  5. Selenium-键盘操作

    在webdriver的Keys类中提供了键盘所有的按键操作,当然也包括一些常见的组合操作如Ctrl+A全选),Ctrl+C(复制),Ctrl+V(粘贴).更多参考官方文档对应的编码http://sel ...

  6. QWidget、QMainWindow、QFrame、QWindow、QDialog、QScrollArea区别

    QWidget是所有可视化控件的基类,可以直接渲染出一个窗口来. QMainWindow用来表示一个主窗口,这个主窗口可以设置菜单和工具栏 QFrame用来表示一个框架,用来当作容器,因为可以设置它的 ...

  7. codeforces 707C C. Pythagorean Triples(数学)

    题目链接: C. Pythagorean Triples time limit per test 1 second memory limit per test 256 megabytes input ...

  8. hdu-5651 xiaoxin juju needs help(数学+gcd约分求阶乘)

    题目链接: xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K ...

  9. Android之ExpandableList扩展用法(基于BaseExpandableListAdapter)

    1.简介 基于基于BaseExpandableListAdapter扩展的ExpandableList用法,现在网上流行的主要有两种:第一种是向BaseExpandableListAdapter传入两 ...

  10. C#实现读写文本文件中的数据

    [1]首先我们定义一段假数据,这里以一个string为例字 static void Main(string[] args)        {            string data = &quo ...