ZOJ 3946 Highway Project 贪心+最短路

题目链接:

http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946

题解:

用dijkstra跑单元最短路径,如果对于顶点v,存在一系列边(ui,v)使得dis[v]最小(dis[v]表示0到v的距离)。这些边能且只能选一条,那么我们自然应该选cost最小的那个边了。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<vector>
  5 #include<algorithm>
  6 #include<queue>
  7 using namespace std;
  8
  9 typedef long long LL;
 10 const int maxn = 1e5 + 10;
 11
 12 struct Edge {
 13     int ne, u, v, c, d;
 14     Edge(int ne, int u, int v, int d, int c) :ne(ne), u(u), v(v), d(d), c(c) {}
 15     Edge() {}
 16 }egs[maxn * 2];
 17
 18 struct Heap {
 19     int u, d, c;
 20     Heap(int u, int d) :u(u), d(d) {}
 21     Heap() {}
 22     bool operator < (const Heap& tmp) const {
 23         return d>tmp.d;
 24     }
 25 };
 26
 27 struct Node {
 28     int u, v, w;
 29     bool operator < (const Node& tmp) const {
 30         return w<tmp.w;
 31     }
 32 }nds[maxn];
 33
 34
 35 int head[maxn], tot;
 36
 37 void addEdge(int u, int v, int d, int c) {
 38     egs[tot] = Edge(head[u], u, v, d, c);
 39     head[u] = tot++;
 40 }
 41
 42 int n, m;
 43 LL ans_d, ans_c;
 44
 45 LL dis[maxn];
 46 //pre[u]记录u和前驱节点的那条边的cost
 47 int pre[maxn];
 48 bool done[maxn];
 49
 50
 51 void dij() {
 52     for (int i = 0; i<n; i++) {
 53         dis[i] = 2e10;
 54     }
 55     memset(done, false, sizeof(done));
 56     priority_queue<Heap> pq;
 57     dis[0] = 0;
 58     pq.push(Heap(0, 0));
 59     while (!pq.empty()) {
 60         Heap x = pq.top(); pq.pop();
 61         int u = x.u;
 62         if (done[u]) continue;
 63         done[u] = true;
 64         int p = head[u];
 65         while (p != -1) {
 66             Edge &e = egs[p];
 67             if (dis[e.v]>dis[u] + e.d) {
 68                 dis[e.v] = dis[u] + e.d;
 69                 pre[e.v] = e.c;
 70                 pq.push(Heap(e.v, dis[e.v]));
 71             }
 72             else if (dis[e.v] == dis[u] + e.d) {
 73                 //这里贪心选cost最小的边
 74                 if (pre[e.v]>e.c)
 75                     pre[e.v] = e.c;
 76             }
 77             p = e.ne;
 78         }
 79     }
 80     ans_d = 0; ans_c = 0;
 81     for (int i = 0; i<n; i++) {
 82         ans_d += dis[i];
 83     }
 84     for (int i = 1; i<n; i++) {
 85         ans_c += pre[i];
 86     }
 87 }
 88
 89 void init() {
 90     memset(head, -1, sizeof(head));
 91     memset(pre, -1, sizeof(pre));
 92     tot = 0;
 93 }
 94
 95 int main() {
 96     //    freopen("data_in.txt","r",stdin);
 97     int tc;
 98     scanf("%d", &tc);
 99     while (tc--) {
100         scanf("%d%d", &n, &m);
101         init();
102         for (int i = 0; i<m; i++) {
103             int u, v, d, c;
104             scanf("%d%d%d%d", &u, &v, &d, &c);
105             addEdge(u, v, d, c);
106             addEdge(v, u, d, c);
107         }
108         dij();
109         printf("%lld %lld\n", ans_d, ans_c);
110     }
111     return 0;
112 }
时间: 2024-12-26 08:39:26

ZOJ 3946 Highway Project 贪心+最短路的相关文章

zoj 3946 Highway Project spfa

题意:一个帝国有 n 个城市,可以在城市两两之间建立 m 条高速公路,建立 x-y 之间的高速路需要时间 d,花费为 c, 最后建立完边(<=m)后使得首都 0 号城市到各个城市(1~n-1)的总时间最少,在多个时间满足条件下再选花费最少的. 思路:直接spfa,当有多个点使得时间最少时,选花费最小的边. #include <iostream> #include <algorithm> #include <string.h> #include <stdio.

ZOJ 3946 Highway Project

迪杰斯特拉最小堆 #include<cstdio> #include<cstring> #include<cmath> #include<map> #include<queue> #include<algorithm> using namespace std; const long long INF=9999999999999; const int maxn=2e5+10; struct X{ int id; long long ti

ZOJ-3946 Highway Project (最短路)

题目大意:一张带权无向图,权有两个参数(d,c),分别表示走过这条边的时间和建造这条边的代价.要求选出一些边,使得0节点到其他点的距离之和最短,并在最短的基础上求最小代价. 题目分析:这是16年浙江省赛的一道题.先求出0到所有点的最短路,然后找出所有可能在最短路径上的边,最后在每一个节点的入边之中都选一条具有最小代价的边.多么简单的一道题!!! 代码如下: # include<iostream> # include<cstdio> # include<cstring>

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy

zoj 1655 Transport Goods (最短路变形)

Transport Goods Time Limit: 2 Seconds      Memory Limit: 65536 KB The HERO country is attacked by other country. The intruder is attacking the capital so other cities must send supports to the capital. There are some roads between the cities and the

hdu1874 畅通project续 最短路 floyd或dijkstra或spfa

Problem Description 某省自从实行了非常多年的畅通project计划后.最终修建了非常多路.只是路多了也不好,每次要从一个城镇到还有一个城镇时,都有很多种道路方案能够选择.而某些方案要比还有一些方案行走的距离要短非常多.这让行人非常困扰. 如今,已知起点和终点.请你计算出要从起点到终点,最短须要行走多少距离. Input 本题目包括多组数据,请处理到文件结束. 每组数据第一行包括两个正整数N和M(0< N<200,0 < M<1000),分别代表现有城镇的数目和已

zoj 3778 Talented Chef 贪心

zoj 3778 Talented Chef 题意: 有n个饼,给出完成每个饼所需要的时间t1,t2,...,tn,现在有m个锅(也就是说可以同时煎m个饼),问完成所有饼至少需要多少时间. 限制: 1 <= n,m,ti <= 40000 思路: 贪心 ans=max(ceil(sigma(1~n,ti)/m),max(ti)) /*zoj 3778 Talented Chef 题意: 有n个饼,给出完成每个饼所需要的时间t1,t2,...,tn,现在有m个锅(也就是说可以同时煎m个饼),问完

ZOJ 2702 Unrhymable Rhymes 贪心

贪心,能凑成一组就算一组 Unrhymable Rhymes Time Limit: 10 Seconds      Memory Limit: 32768 KB      Special Judge An amateur poet Willy is going to write his first abstract poem. Since abstract art does not give much care to the meaning of the poem, Willy is plan

ZOJ 3689 Digging(贪心+dp)

Digging Time Limit: 2 Seconds      Memory Limit: 65536 KB When it comes to the Maya Civilization, we can quickly remind of a term called the end of the world. It's not difficult to understand why we choose to believe the prophecy (or we just assume i