1003. Emergency (25)
2015-08-26 15:28
239 查看
1003. Emergency (25)
时间限制400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked
on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.
Input
Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in
and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected
by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.
Output
For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.
Sample Input
5 6 0 2 1 2 1 5 3 0 1 1 0 2 2 0 3 1 1 2 1 2 4 1 3 4 1
Sample Output
2 4
一开始我不会做的一道题,典型的多权最短路,同时还要统计最短路有多少条,这里只要明白几个状态之间的关系就好了
当用A点更新B点的距离时,如果B点的距离变短了,那么
RoadCnt[a] = RoadCnt[b];
SumfFireworker[b] = SumofFireworker[a] + Fireworker[a];
如果更新时两者恰好相等,
RoadCnt[a]
+= RoadCnt[b];
SumfFireworker[b]
= max(SumfFireworker[b],SumofFireworker[a] + Fireworker[a]);
其他的地方注意一点细节就好了,今天重写了一下这道题目,学会了algorithm中fill的用法,算是一个收获吧,他比memset适用范围更广。
当然这道题目还有其他解法,比如第一步单纯求最短路,但同时用perv向量记录父亲节点,这样的话dijkstra之后我们就会获得一张仅由最短路的图,再从终点到起点进行深度优先搜索就OK了,第二种方法代码上虽然较多,但是实现起来逻辑更清晰,而且能解决的问题范围更广。
# include <cstdio> # include <vector> # include <queue> # include <algorithm> # include <climits> using namespace std; const int size = 505; typedef pair<int,int> edge;/*<to,dist>*/ typedef pair<int,int> point; /*<dist,cityNo>*/ struct vertex{ vector<edge> e; }; int Fw[size]; int FwSum[size]; int RoadCnt[size]; vertex city[size]; int main() { int i,j,tmp; int n,m,s,e; scanf("%d%d%d%d",&n,&m,&s,&e); for (i=0;i<n;i++) scanf("%d",Fw+i); for (i=0;i<m;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); city[a].e.push_back(edge(b,c)); city[b].e.push_back(edge(a,c)); } /**dijkstra**/ priority_queue<point,vector<point>,greater<point> > pri_que; int dist[size]; fill(dist,dist+n,INT_MAX>>1); fill(RoadCnt,RoadCnt+n,1); dist[s] = 0; FwSum[s] = Fw[s]; pri_que.push(point(dist[s],s)); while (!pri_que.empty()) { point loca = pri_que.top();pri_que.pop(); if (dist[loca.second] < loca.first) continue; vector<edge>& adj = city[loca.second].e; for (i=0;i<adj.size();i++) if (dist[adj[i].first] > dist[loca.second] + adj[i].second) { dist[adj[i].first] = dist[loca.second] + adj[i].second; pri_que.push(point(dist[adj[i].first],adj[i].first)); RoadCnt[adj[i].first] = RoadCnt[loca.second]; FwSum[adj[i].first] = FwSum[loca.second] + Fw[adj[i].first]; } else if (dist[adj[i].first] == dist[loca.second] + adj[i].second) { RoadCnt[adj[i].first] += RoadCnt[loca.second]; FwSum[adj[i].first] = max(FwSum[adj[i].first],FwSum[loca.second] + Fw[adj[i].first]); } } /**dijkstra**/ printf("%d %d\n",RoadCnt[e],FwSum[e]); return 0; }
相关文章推荐
- JSTL自定义函数标签
- 黑马程序员 多线程
- 有时候,事情就是很简单,可是你就是找不到原因
- 201508261524_《JavaScript之浮点数算数运算》
- 数据库中的日期“定制”问题(去掉select也可以用来处理日期)
- 学习笔记009之布局
- Backbone
- IOS tableView通过代码自定义cell
- 黄波老师微信公众号“每天一点”正式开通啦
- 什么是4K电视
- HDU-2553-N皇后问题
- 使用mysqldump备份数据库(含存储过程)
- MII接口全家福
- virt-install参数说明
- HDU 5407 CRB and Candies
- git pull file name too long
- Mybatis + SpringMVC事务管理
- HDU1213 How Many Tables - 并查集
- Java- 装箱、枚举、注解
- (2)如何在Javascript中自定义类 & 面向对象编程 (封装)