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. 深入理解Java执行时数据区

    前情回想 在本专栏的前12篇博客中. 我们主要大致介绍了什么是JVM, 而且具体介绍了class文件的格式. 对于深入理解Java, 或者深入理解运行于JVM上的其它语言, 深入理解class文件格式 ...

  2. Js_闭包详解

    http://blog.csdn.net/chenglc1612/article/details/53413318 一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变 ...

  3. YTU 2911: 我想放假

    2911: 我想放假 时间限制: 1 Sec  内存限制: 128 MB 提交: 124  解决: 46 题目描述 小明的弟弟上小学了,每次刚入学就想知道什么时候放假,但是每学期开学的日子和每学期的有 ...

  4. 织梦仿站列表页pagelist分页显示竖排,如何修改成横排?

    织梦仿站列表页pagelist分页显示竖排,如何修改成横排? 织梦列表页的分页标签是采用pagelist来进行调用的,但是很多人在调用之后会出现一个列表竖着排列的问题(横排美观度好一些),还是非常不美 ...

  5. java代码实现JDBC连接MySql以及引用驱动程序包

    JDBC链接MySql     JDBC链接MySql的话题已经老掉牙了,这次我只想通过使用简洁的代码实现,采用封装的思想,将链接MySql的代码封装在类的静态方法中,供一次性调用返回java.sql ...

  6. 并不对劲的fhq treap

    听说很对劲的太刀流不止会splay一种平衡树,并不对劲的片手流为了反驳他,并与之针锋相对,决定学学高端操作. 很对劲的太刀流-> 据说splay常数极大,但是由于只知道splay一种平衡树能对序 ...

  7. 杂项-Java:JBoss

    ylbtech-杂项-Java:JBoss 是一个基于J2EE的开放源代码的应用服务器. JBoss代码遵循LGPL许可,可以在任何商业应用中免费使用.JBoss是一个管理EJB的容器和服务器,支持E ...

  8. Markdown——让你专注写作

    Markdown--让你专注写作 前些日子,写作的时候总会因为排版而耽误时间,甚至因为排版而把写作的专注力转移到了貌似相关的排版上.诚然,一个好的排版,会让读者有良好的体验,可是对于写作的人来说,这却 ...

  9. 待研究———node中使用session时的id不断更改问题

    使用的expree,中间件为cookie-parser,express-session,当对res.session.id进行赋值操作后,再调取其值会发现,此时它的值并不是最初给定的值,而是经过加密的字 ...

  10. json 获取属性值

    ajax后台获取json数据 前台赋值.由于值太多 一个个写 val会类似的.因为直接字段值和 前台的标签id相同,这样只要循环结果集json赋值即可. 这里需要用到json的字段值 var data ...