ZOJ 3946 Highway Project(多属性边权最短路)
来源:互联网 发布:java与安卓 编辑:程序博客网 时间:2024/06/01 08:40
题目链接:
题意:
有n个城市编号从0–n-1,其中编号为0的城市是首都,有m条无向边,每条边有两个属性走过这条路径的time和建造这条路径的cost,求从首都出发到达其他各个城市的最少时间和最少cost?(优先时间最少)
分析;
首先需要明确在满足到达各个城市最少时间情况下若干条边的cost之和而不是到达各个城市的cost之和!
最少时间是求一次最短路,然后在求最短路的时候判断如果有多条最短路取边cost权最少的一个作为到达这个城市的cost,最终答案是各个城市的time和cost累加。(time是从源点出发到达这个城市的最少时间)
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <climits>#include <cmath>#include <ctime>#include <cassert>#include <queue>#define IOS ios_base::sync_with_stdio(0); cin.tie(0);using namespace std;typedef long long ll;const int MAX_N = 100010;int T, n, m, total;int vis[MAX_N], head[MAX_N * 2];struct Edge{ int to, next; ll cost, tim;}edge[MAX_N * 2];struct HeapNode{ int u; ll tim, cost; bool operator < (const HeapNode& rhs) const { if(tim != rhs.tim) return tim > rhs.tim; else return cost > rhs.cost; } };HeapNode ans[MAX_N];void AddEdge(int from, int to, ll tim, ll cost){ edge[total].to = to; edge[total].tim = tim; edge[total].cost = cost; edge[total].next = head[from]; head[from] = total++;}void Dijkstra(){ priority_queue <HeapNode> Q; while(!Q.empty()) Q.pop(); for(int i = 0; i < n; i++){ ans[i].u = i; ans[i].tim = ans[i].cost = (ll)INT_MAX * 100; //初始化一开始是INT_MAX,结果WA了好多发!!! } ans[0].tim = ans[0].cost = 0; Q.push(ans[0]); memset(vis, 0, sizeof(vis)); while(!Q.empty()){ HeapNode x = Q.top(); Q.pop(); int u = x.u; if(vis[u]) continue; vis[u] = 1; for(int i = head[u]; i != -1; i = edge[i].next){ int v = edge[i].to; ll t = edge[i].tim; ll c = edge[i].cost; if(ans[v].tim > ans[u].tim + t || (ans[v].tim == ans[u].tim + t && ans[v].cost > c)){ ans[v].tim = ans[u].tim + t; ans[v].cost = c; //cout << u << "-->" << v << ":" << ans[v].tim << endl; Q.push(ans[v]); } } }}int main(){ IOS; cin >> T; while(T--){ cin >> n >> m; memset(head, -1, sizeof(head)); total = 0; for(int i = 0; i < m; i++){ int a, b; ll c, d; cin >> a >> b >> c >> d; AddEdge(a, b, c, d); AddEdge(b, a, c, d); } Dijkstra(); ll res1 = 0, res2 = 0; for(int i = 1; i <= n - 1; i++){ res1 += ans[i].tim; res2 += ans[i].cost; } cout << res1 << " " << res2 << endl; } return 0;}
0 0
- ZOJ 3946 Highway Project(多属性边权最短路)
- ZOJ 3946 Highway Project 单源最短路
- ZOJ-3946-Highway Project(最短路)
- ZOJ 3946 Highway Project(最短路)
- [最短路应用] ZOJ 3946 Highway Project
- ZOJ 3946Highway Project
- zoj 3946 Highway Project
- ZOJ 3946 Highway Project
- ZOJ 3946 Highway Project
- zoj-3946-Highway Project【13th浙江省赛】【最短路】
- ZOJ 3946 Highway Project(spfa最短路+记忆化搜索)
- zoj 3946Highway Project(最短路改)
- ZOJ 3946Highway Project【spfa】
- ZOJ 3946 Highway Project(Dijkstra)
- ZOJ 3946 Highway Project (spfa)
- ZOJ 3946-Highway Project【最短路的应用】(2016浙江省大学生程序设计竞赛)
- ZOJ 3946 Highway Project(最小费用最短路,优先队列优化dijkstra)
- ZJOJ Highway Project 3946(单源最短路+贪心)
- Java编程思想第四版第六章学习——访问权限设置
- intellij idea无法访问静态资源
- python连接HBase
- day5.17总结_加载图片(圆圈和压缩、一级、二级缓存)
- 矩阵的三角分解算法实验报告
- ZOJ 3946 Highway Project(多属性边权最短路)
- Java设计模式之代理模式
- 改变DM6467的内存划分
- 设置FLAG_ACTIVITY_NEW_TASK导致Activity打开两次
- POJ——1195Mobile phones(二维树状数组点修改矩阵查询)
- mina服务关闭
- java ATM
- 2732: [HNOI2012]射箭
- 烷烃计数