PAT (Advanced level) 1003. Emergency (25) Dijkstra
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 题意:求从C1 到 C2的最短路的方案数及最大权值。
题解:dijkstra。当距离相同时记得叠加方案数。
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>
#include <string.h>
#define MAXX 100010
#define MMF(x) memset(x, 0, sizeof(x))
#define MMI(x) memset(x, INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = ; struct sion
{
int dic;
int amt;
int pcnt;
}C[N];
int mp[N][N];
bool vis[N];
int team[N]; void init(int &n)
{
MMF(vis);
for(int i = ; i < n; i++)
{
C[i].dic = INF;
C[i].pcnt = ;
C[i].amt = ;
for(int j = ; j < n; j++)
mp[i][j] = INF;
} }
void dijkstra(int &s, int &n)
{
queue<int>q;
vis[s] = ;
C[s].dic = ;
C[s].amt = team[s];
q.push(s);
//
while(!q.empty())
{
//cout << "~";
int now = q.front();
q.pop();
for(int i = ; i < n; i++)
{
if(!vis[i])
{ if(C[i].dic > C[now].dic + mp[now][i])
{
C[i].dic = C[now].dic + mp[now][i];
C[i].amt = C[now].amt + team[i];
C[i].pcnt = C[now].pcnt;
}
else if(C[i].dic == C[now].dic + mp[now][i])
{
C[i].pcnt += C[now].pcnt;
if(C[i].amt < C[now].amt + team[i])
C[i].amt = C[now].amt + team[i];
}
}
}
int mi = INF;
int x;
for(int i = ; i < n; i++)
{
if(!vis[i] && C[i].dic < mi)
{
mi = C[i].dic;
x = i;
}
//cout << C[i].dic << endl;
}
if(mi == INF)
break;
q.push(x);
vis[x] = ;
}
return ;
} int main()
{
int n, m, s, t;
int x, y, z;
scanf("%d%d%d%d", &n, &m, &s, &t);
for(int i = ; i < n; i++)
scanf("%d", &team[i]);
init(n);
for(int i = ; i < m; i++)
{
scanf("%d%d%d", &x, &y, &z);
if(mp[x][y] >= z)
mp[x][y] = mp[y][x] = z;
}
dijkstra(s, n); printf("%d %d\n", C[t].pcnt, C[t].amt); }
PAT (Advanced level) 1003. Emergency (25) Dijkstra的更多相关文章
- PAT (Advanced Level) 1003. Emergency (25)
最短路+dfs 先找出可能在最短路上的边,这些边会构成一个DAG,然后在这个DAG上dfs一次就可以得到两个答案了. 也可以对DAG进行拓扑排序,然后DP求解. #include<iostrea ...
- PAT 解题报告 1003. Emergency (25)
1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special map of yo ...
- PAT Advanced 1003 Emergency (25) [Dijkstra算法]
题目 As an emergency rescue team leader of a city, you are given a special map of your country. The ma ...
- PTA (Advanced Level) 1003 Emergency
Emergency As an emergency rescue team leader of a city, you are given a special map of your country. ...
- PAT (Advanced Level) 1078. Hashing (25)
二次探测法.表示第一次听说这东西... #include<cstdio> #include<cstring> #include<cmath> #include< ...
- PAT (Advanced Level) 1070. Mooncake (25)
简单贪心.先买性价比高的. #include<cstdio> #include<cstring> #include<cmath> #include<vecto ...
- PAT (Advanced Level) 1029. Median (25)
scanf读入居然会超时...用了一下输入挂才AC... #include<cstdio> #include<cstring> #include<cmath> #i ...
- PAT (Advanced Level) 1010. Radix (25)
撸完这题,感觉被掏空. 由于进制可能大的飞起..所以需要开longlong存,答案可以二分得到. 进制很大,导致转换成10进制的时候可能爆long long,在二分的时候,如果溢出了,那么上界=mid ...
- PAT (Advanced Level) 1032. Sharing (25)
简单题,不过数据中好像存在有环的链表...... #include<iostream> #include<cstring> #include<cmath> #inc ...
随机推荐
- lintcode-176-图中两个点之间的路线
176-图中两个点之间的路线 给出一张有向图,设计一个算法判断两个点 s 与 t 之间是否存在路线. 样例 如下图: for s = B and t = E, return true for s = ...
- javaweb引用jar类库的问题
JAVAWEB中,需要引用的jar类库必须放在/WebContent/WEB-INF/lib文件夹中.不然就会各种报错.
- lol人物模型提取(七)
9月13号我就把上了贴图的模型文件发了过去,到9月18号他们那的颜色就上好了,一个叫"3d打印旗舰店"的人加了我微信并拍了几张照片发了给我,效果图如下: 第一眼看上去我还是 ...
- laravel 可用验证规则
accepted 验证的字段必须为 yes. on. 1.或 true.这在确认服务条款是否同意时相当有用. active_url 相当于使用了 PHP 函数 dns_get_record,验证的字段 ...
- bwapp之xss(blog)
存储型XSS,持久化,代码是存储在服务器中的,如在个人信息或发表文章等地方,加入代码,如果没有过滤或过滤不严,那么这些代码将储存到服务器中,用户访问该页面的时候触发代码执行.这种XSS比较危险,容易造 ...
- centOS6.5如何从启动界面直接进入命令行界面
进入系统后,按Ctrl+Alt+Fn可以切换控制台,其中F1~F6是字符控制台,F7~F12是X控制台 如果启动直接进入字符控制台,而不是X Window,可以编辑/etc/inittab将id:5: ...
- GC是什么?为什么要有GC
GC:Garbage Collection 垃圾收集器. GC就是对“不可达“的对象进行回收,释放内存. Java内存的管理实际上就是对对象的管理,其中包括对对象的分配和回收. 对于程序员来说,分配对 ...
- ASP.NET MVC4计划任务实现方法(定时执行某个功能)
系统中定时执行某个任务是比较常用的功能,如一个部门定期向上级部门上报数据是一个典型的例子,下面就简单说说在.net mvc中如何实现定时执行某个功能的方法. 1.首先修改Glocal.asax文件,在 ...
- 【python】如何查看已经安装的python软件包和版本
pip 是一个安装和管理 Python 包的工具 , 是 easy_install 的一个替换品. pip freeze可以查看已经安装的python软件包和版本 pip list 也可以
- Activiti5工作流笔记四
排他网关(ExclusiveGateWay) 流程图 部署流程定义+启动流程实例 查询我的个人任务 完成我的个人任务 并行网关(parallelGateWay) 流程图 部署流程定义+启动流程实例 查 ...