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

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

代码如下:

#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. wget 实现web监控脚本

    #!/bin/sbin timeout= times= url=https://1.1.1.1 while true;do wget --no-check-certificate --timeout= ...

  2. WebSocket实现Web聊天室

    一.客户端: JS代码如下: /* * 这部分js将websocket封装起来 */ var websocket = null; //判断当前浏览器是否支持WebSocket if ('WebSock ...

  3. 暑假集训第一周比赛C题

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83146#problem/C C - 学 Crawling in process... C ...

  4. 好久没更了,确实太忙了--dedecms篇

    最近写了一个地方新闻网站,可以看看:www.qiluhuabao.com.模仿www.bashan.com写的 用的是dedecms,只听过,没用过.终于在上周来了一个必须用到cms的项目,现学现卖, ...

  5. 大话设计模式--代理模式 proxy

    1. 代理模式: 为其他对象提供一种代理以控制这个对象的访问. 代理模式使用场合: a. 远程代理, 为一个对象在不同的地址空间提供局部代理,隐藏一个对象存在于不同地址空间的事实.如.net中WebS ...

  6. git use

    git init git init --bare  ''会将文档直接加上.git后缀 具体内容请渡 git add --all git commit -m 'label' git push --all ...

  7. BZOJ 2431 [HAOI2009]逆序对数列:dp 逆序对

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2431 题意: 给定n,k,问你有多少个由1~n组成的排列,使得逆序对个数恰好为k个. 题解 ...

  8. Java企业微信开发_07_JSSDK多图上传

    一.本节要点 1.1可信域名 所有的JS接口只能在企业微信应用的可信域名下调用(包括子域名),可在企业微信的管理后台“我的应用”里设置应用可信域名.这个域名必须要通过ICP备案,不然jssdk会配置失 ...

  9. php 冒泡排序原理

    $start = microtime(true);   $popArr = array(6,3,23,1,5,100,399,99,66);   echo '6,3,23,1,5,100,399,99 ...

  10. hihocoder-1285 智力竞赛(区间dp)

    智力竞赛 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi.小Ho还有被小Hi强拉来的小Z,准备组队参加一个智力竞赛.竞赛采用过关制,共计N个关卡.在第i个关卡中,小 ...