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. Win7下安装iMac系统

    首先是灰常激动啊,一下午的努力最终在自己华硕的笔记本上安装了mac系统. 先上一个成果截图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveXl4aGh4/ ...

  2. shell脚本常用(记)

    1.变量检查,判空 a.直接变量判断  if [ ! $1 ];then ..empty..fi b.变量通过" "引号引起来  if [ ! -n "$1" ...

  3. 一.OC基础之:1,OC语言的前世今生 ,2,OC语言入门,3,OC语言与C的差异,4,面向对象,5,类和对象的抽象关系,6,类的代码创建,7,类的成员组成及访问

    1,OC语言的前世今生 , 一, 在20世纪80年代早期,布莱德.麦克(Brad Cox)设计了OC语言,它在C语言的基础上增加了一层,这意味着对C进行了扩展,从而创造出一门新的程序设计语言,支持对象 ...

  4. HTML标签防XSS攻击过滤模块--待优化

    HTML标签防XSS攻击过滤模块 http://cnodejs.org/topic/5058962f8ea56b5e7806b2a3

  5. PHP编程常见小错误错误

    使用PHP的过程中经常因为粗心出一些简单的错误,先将自己遇到几个的记录下来,以后慢慢增加. 1 Fatal error: Function name must be a string 翻译很简单,就是 ...

  6. Pascal输出星星

    program Project2; {$APPTYPE CONSOLE} uses SysUtils; var i,j:integer; begin { TODO -oUser -cConsole M ...

  7. property_get 与 property_set 的返回值(转载)

    转自:http://wzw19191.blog.163.com/blog/static/13113547020103218265162/ /* property_get: returns the le ...

  8. HDU1452:Happy 2004(积性函数)(因子和)

    题意 给出\(x\),求\(2004^x\)的所有因子和 分析 \(2004=2*2*3*167\) 则\(2004^x\)=\(2^{2x}*3^x*167^x\) s[\(2004^x\)]=s[ ...

  9. P4055 [JSOI2009]游戏

    传送门 把这个图给黑白染色然后建二分图,如果有完备匹配那么就gg,否则放在所有的非匹配点都可以 简单来说的话就是放在非匹配点,那么对手的下一步必定移到一个匹配点,然后自己可以把它移到这个匹配点所匹配的 ...

  10. cenos mkdir 无法创建文件夹,即便文件权限为777

    SELinux 拒绝了httpd的方式去读写此目录 chcon -R -t httpd_sys_content_rw_t /var/www/html