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 ...
随机推荐
- nodejs基础学习
一:复制官网的代码,建立一个简单的服务器 const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; ...
- python异步初步窥探
1.异步之难:因为其执行吮吸不可预料,当下正要发生什么事件不可预料. 程序下一步行为往往依赖上一步值执行结果,如何知晓上次异步调用已完成并获取结果, 回调成了必然选择,那又 ...
- YaoLingJump开发者日志(六)
作为一只天才魔法少女狐,不会魔法怎么行?于是我给瑶玲增加了一个技能:魔法弹. 当然,能使用魔法的前提是得有个魔杖,像这样: 魔杖不仅能让瑶玲使用魔法,当瑶玲被攻击时还能提供2s的无敌状态: ...
- Redis 学习之主从复制
该文使用centos6.5 64位 redis3.2.8 主从复制 Redis的复制功能是支持多个数据库之间的数据同步.一类是主数据库(master)一类是从数据库(slave),主数据库可以进 ...
- ICE checkbox 用法
Hello everybody, I have a datable which contain multiple lines gotten from database, in the header o ...
- vue-cli项目打包出现空白页和路径错误问题
vue-cli项目打包: 1. 命令行输入:npm run build 打包出来后项目中就会多了一个文件夹dist,这就是我们打包过后的项目. 第一个问题,文件引用路径.我们直接运行打包后的文件夹 ...
- 使用canvas控制gif图片的播放与暂停
if ('getContext' in document.createElement('canvas')) { HTMLImageElement.prototype.play = function() ...
- CentOS 压缩(打包)和解压
1.tar命令 -c 创建压缩文件 -x 解开压缩文件 -t 查看压缩包内有哪些文件 -z 用 Gzip压缩或解压 -j 用 bzip2压缩或解压 -v 显示压缩或解压的过程 -f 目标文件名,在 f ...
- 【luogu2181】对角线
首先由于不会有三条对角线交于一点,所以过某一个交点有且只能有2条对角线 而这两条对角线实质上是确定了4个顶点(也可以看做是一个四边形的两条对角线交于一点,求四边形的数量). 因此我们只需要确定4个顶点 ...
- POJ3415:Common Substrings——题解
http://poj.org/problem?id=3415 给定两个字符串A 和B,求长度不小于k 的公共子串的个数(可以相同). 论文题,和上道题(POJ2774)类似,首先想到现将AB串合并,然 ...