13th省赛K
2016-04-27 20:23
459 查看
Highway Project
Time Limit: 2 Seconds Memory Limit: 65536 KB
Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can reach other cities from the capital as fast as possible. Thus, he proposed the highway
project.
The Marjar Empire has N cities (including the capital), indexed from 0 to N - 1 (the capital is 0) and there are M highways can be built. Building the i-th
highway costs Ci dollars. It takes Di minutes to travel between city Xi and Yi on the i-th highway.
Edward wants to find a construction plan with minimal total time needed to reach other cities from the capital, i.e. the sum of minimal time needed to travel from the capital to city i (1
≤ i ≤ N). Among all feasible plans, Edward wants to select the plan with minimal cost. Please help him to finish this task.
The first contains two integers N, M (1 ≤ N, M ≤ 105).
Then followed by M lines, each line contains four integers Xi, Yi, Di, Ci (0 ≤ Xi, Yi < N,
0 < Di, Ci < 105).
题目大意:
问从0节点到所有节点的最短步数,如果步数相同,那么输出步数最短的并且花费最少的路径
思路
dijskstra就好了。(表示inf设置小了。。。卡了我三个小时。。。)
Time Limit: 2 Seconds Memory Limit: 65536 KB
Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can reach other cities from the capital as fast as possible. Thus, he proposed the highway
project.
The Marjar Empire has N cities (including the capital), indexed from 0 to N - 1 (the capital is 0) and there are M highways can be built. Building the i-th
highway costs Ci dollars. It takes Di minutes to travel between city Xi and Yi on the i-th highway.
Edward wants to find a construction plan with minimal total time needed to reach other cities from the capital, i.e. the sum of minimal time needed to travel from the capital to city i (1
≤ i ≤ N). Among all feasible plans, Edward wants to select the plan with minimal cost. Please help him to finish this task.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:The first contains two integers N, M (1 ≤ N, M ≤ 105).
Then followed by M lines, each line contains four integers Xi, Yi, Di, Ci (0 ≤ Xi, Yi < N,
0 < Di, Ci < 105).
Output
For each test case, output two integers indicating the minimal total time and the minimal cost for the highway project when the total time is minimized.Sample Input
2 4 5 0 3 1 1 0 1 1 1 0 2 10 10 2 1 1 1 2 3 1 2 4 5 0 3 1 1 0 1 1 1 0 2 10 10 2 1 2 1 2 3 1 2
Sample Output
4 3 4 4
题目大意:
问从0节点到所有节点的最短步数,如果步数相同,那么输出步数最短的并且花费最少的路径
思路
dijskstra就好了。(表示inf设置小了。。。卡了我三个小时。。。)
#include using namespace std; typedef long long ll; const ll inf = 1e18; const int maxn = 100000 + 100; struct edge{ int u, v; ll d, c; edge(int u, int v, ll d, ll c): u(u), v(v), d(d), c(c){} }; vector e; vector G[maxn]; struct node{//c是价格,d是时间 int u; ll d, c ; node (int u, ll d, ll c): u(u), d(d), c(c){} bool operator < (const node &a) const{ if (a.d != d) return d > a.d; return c > a.c; } }; ll d[maxn]; ll cost[maxn]; int n, m; bool vis[maxn]; void init(){ scanf("%d%d", &n, &m); for (int i = 0; i < m; i++){ int u, v; ll d, c; cin >> u >> v >> d >> c; e.push_back(edge(u, v, d, c)); e.push_back(edge(v, u, d, c)); int l = e.size(); G[u].push_back(l - 2); G[v].push_back(l - 1); } } void solve(){ for (int i = 0; i <= n; i++){ d[i] = inf; cost[i] = inf; vis[i] = false; } priority_queue q; d[0] = 0; cost[0] = 0; q.push(node(0, d[0], cost[0])); while (!q.empty()){ node x = q.top(); q.pop(); int u = x.u;//现在所处的顶点 if (vis[u] == true) continue; vis[u] = true; for (int i = 0; i < G[u].size(); i++){ edge y = e[G[u][i]]; if (d[y.v] > d[u] + y.d){ cost[y.v] = y.c; d[y.v] = y.d + d[u]; q.push(node(y.v, d[y.v], cost[y.v])); } else if (d[y.v] == d[u] + y.d && cost[y.v] > y.c){ cost[y.v] = y.c; q.push(node(y.v, d[y.v], cost[y.v])); } } } ll resc = 0, resd = 0; for (int i = 1; i < n; i++){ resc += cost[i]; resd += d[i]; } cout << resd << " " << resc << endl; for (int i = 0; i < n; i++){ G[i].erase(G[i].begin(), G[i].end()); } e.erase(e.begin(), e.end()); } int main(){ int t; scanf("%d", &t); while (t--){ init(); solve(); } return 0; }
相关文章推荐
- Android常用工具类
- 单例模式与双重检测
- 苹果夺走了产业链的尊严?
- 『 Spark 』2. spark 基本概念解析
- 解决OnscrollListener中Listview.getChildAt()为null问题
- makefile中条件判断与函数
- bash快捷键
- [被喷了6年的帖子]PHP、CakePHP哪凉快哪呆着去
- CodeForces - 236A Boy or Girl (模拟)水
- 关于Bitmap的二次采样
- 设计模式(14)之桥接模式
- ES5中新增的Array方法详细说明
- Spark中组件Mllib的学习2之MovieLensALS学习(集群run-eaxmples运行)
- 学习软件工程的知名网站
- 【VR】如何用Gear VR打造虚拟现实
- sort函数的使用
- Shell 脚本知识回顾 (四) —— Shell 命令及Shell 相关语句
- 6.DNS公司PC访问外网的设置 + 主DNS服务器和辅助DNS服务器的配置
- 漫画城之引导页+主页
- centos7搭建vsftpd环境详解(亲测)