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 

题意:每个点有点权,每条边有边权,问一个点到另一个点的最短路径(路径上的边权和)有多少条,所有最短路径中路径点权和最大为
多少。 思路:dijkstra求最短路,再dfs找条数和最大点权和。 浙大的陈越老师出的题,会包含很多情况,锻炼自己考虑完善的能力。 总结:关于图的问题,若题中没特意说明,两点之间可以有多条权值相同或不同的边(用邻接表比较好)。在这道题中,起点和终点应该可以相同。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<vector>
using namespace std;
#define N 505
#define INF 999999999 struct Eage
{
int v,val,next;
}eage[N*N];
int head[N],cnte; void addEage(int a,int b,int v)
{
eage[cnte].v=b;
eage[cnte].val=v;
eage[cnte].next=head[a];
head[a]=cnte++;
} int dist[N],cost[N],n,m,val[N];
bool vis[N];
int res,cntr; void dijkstra(int c1,int c2)
{
for(int i=;i<n;i++)
{
cost[i]=val[i];
dist[i]=INF;
vis[i]=;
}
for(int i=head[c1];i!=;i=eage[i].next)
{
int u=eage[i].v,v=eage[i].val;
if(dist[u]>v)
dist[u]=v;
}
vis[c1]=;
dist[c1]=;
for(int i=;i<n;i++)
{
int minn=INF,u;
for(int j=;j<n;j++)
if(minn>dist[j]&&vis[j]==)
{
minn=dist[j];
u=j;
}
vis[u]=;
for(int j=head[u];j!=;j=eage[j].next)
{
int v=eage[j].v,va=eage[j].val;
if(dist[v]>dist[u]+va) dist[v]=dist[u]+va;
}
}
} bool vis1[N];
void dfs(int c1,int c2,int len,int sum)
{
if(len>dist[c2])
return;
if(c1==c2)
{
cntr++;
res=max(res,sum);
return;
}
for(int i=head[c1];i!=;i=eage[i].next)
{
int v=eage[i].v,va=eage[i].val;
if(vis1[v]==)
{
vis1[v]=;
dfs(v,c2,len+va,sum+val[v]);
vis1[v]=;
}
}
} int main()
{
int c1,c2;
scanf("%d%d%d%d",&n,&m,&c1,&c2);
for(int i=;i<n;i++)
scanf("%d",&val[i]);
cnte=;
for(int i=;i<m;i++)
{
int a,b,v;
scanf("%d%d%d",&a,&b,&v);
addEage(a,b,v);
addEage(b,a,v);
}
if(c1==c2)
{
printf("1 %d\n",val[c1]);
return ;
}
cntr=;
dijkstra(c1,c2);
res=;
vis1[c1]=;
dfs(c1,c2,,val[c1]);
printf("%d %d\n",cntr,res);
return ;
} /*
4 3 3 2
1 2 0 4
0 1 1
1 2 3
2 3 6 4 4 0 3
1 2 5 7
0 1 1
0 2 1
1 3 1
2 3 1 2 3 0 1
2 3
0 1 3
0 1 2
1 0 2 */

patest_1003_Emergency (25)_(dijkstra+dfs)的更多相关文章

  1. 天梯赛练习 L3-011 直捣黄龙 (30分) dijkstra + dfs

    题目分析: 本题我有两种思路,一种是只依靠dijkstra算法,在dijkstra部分直接判断所有的情况,以局部最优解得到全局最优解,另一种是dijkstra + dfs,先计算出最短距离以及每个点的 ...

  2. BZOJ_4867_[Ynoi2017]舌尖上的由乃_分块+dfs序

    BZOJ_4867_[Ynoi2017]舌尖上的由乃_分块+dfs序 Description 由乃为了吃到最传统最纯净的美食,决定亲自开垦一片菜园.现有一片空地,由乃已经规划n个地点准备种上蔬菜.最新 ...

  3. 【bzoj4016】[FJOI2014]最短路径树问题 堆优化Dijkstra+DFS树+树的点分治

    题目描述 给一个包含n个点,m条边的无向连通图.从顶点1出发,往其余所有点分别走一次并返回. 往某一个点走时,选择总长度最短的路径走.若有多条长度最短的路径,则选择经过的顶点序列字典序最小的那条路径( ...

  4. PAT-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+DFS

    PAT 1030 最短路最小边权 堆优化dijkstra+DFS 1030 Travel Plan (30 分) A traveler's map gives the distances betwee ...

  5. PAT-1018 Public Bike Management(dijkstra + dfs)

    1018. Public Bike Management There is a public bike service in Hangzhou City which provides great co ...

  6. 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs

    前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...

  7. BZOJ_4765_普通计算姬_分块+dfs序+树状数组

    BZOJ_4765_普通计算姬_分块 Description "奋战三星期,造台计算机".小G响应号召,花了三小时造了台普通计算姬.普通计算姬比普通计算机要厉害一些 .普通计算机能 ...

  8. 1030 Travel Plan Dijkstra+dfs

    和1018思路如出一辙,先求最短路径,再dfs遍历 #include <iostream> #include <cstdio> #include <vector> ...

  9. PAT1018 (dijkstra+dfs)

    There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...

随机推荐

  1. Codeforces Round #261 (Div. 2)——Pashmak and Graph

    题目链接 题意: n个点.m个边的有向图.每条边有一个权值,求一条最长的路径,使得路径上边值严格递增.输出路径长度 )) 分析: 由于路径上会有反复点,而边不会反复.所以最開始想的是以边为状态进行DP ...

  2. HDU1754 —— I Hate It 线段树 单点修改及区间最大值

    题目链接:https://vjudge.net/problem/HDU-1754 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜 ...

  3. 闲得蛋疼,JavaScript版本BadApple

    参考Vim版本的BadApple改写而成.由于加载数据比较大,可能网速不给力的童鞋效果不太好,多刷新几次就好了,^_^.运行环境:支持HTML5 Canvas的浏览器.​1. 代码:$(functio ...

  4. html5--switch选择结构的优化

    html5--switch选择结构的优化 问题: 使用循环语句判断月份是31天还是30天 两点提示: 使用switch多条件判断语句 合理的省略break优化代码 <!DOCTYPE html& ...

  5. 地图上显示X,Y 坐标代码

    事件数据 所有的鼠标事件都使用MouseButtonEventArgs和MouseEventArgs作为事件数据,通过这两个参数可以获取相关事件数据,使用GetPosition方法或者Source.H ...

  6. JS获得本月的第一天和最后一天

    <script> //本月第一天  function showFirstDay()  {      var Nowdate=new Date();      var MonthFirstD ...

  7. ubuntu安装IDEA和PYCHARM

    IDEA和PYCHAR的下载以及安装步骤一样. 1.下载免费学习版本(Community) 2.解压文件到opt文件夹下面sudo tar -zxvf xxx -C /opt 3.进入解压之后的bin ...

  8. bzoj 2132 圈地计划【最小割+dinic】

    对于网格图,尤其是这种要求相邻各自不同的,考虑黑白染色 对于这张染色后图来说: 对于每个黑格: 表示初始时选择商业区: s点向它连商业区收益的流量,它向t点连工业区收益的流量: 割断S侧的边说明反悔, ...

  9. P5089 [eJOI2018]元素周期表(并查集)

    传送门 以后看到棋盘要么黑白染色要么二分图! 我们考虑对行列建二分图,如果\(i\)行\(j\)列有,就把\(i\)和\(j+n\)连起来 我们要让它变成一张完全二分图.考虑条件\((i_1,j_1+ ...

  10. _bzoj1503 [NOI2004]郁闷的出纳员【Splay】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1503 由于初始工资未达到下限而离开的员工不算在离开人数之内...坑爹... 然后就是写kth ...