patest_1003_Emergency (25)_(dijkstra+dfs)
1003. Emergency (25)
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)的更多相关文章
- 天梯赛练习 L3-011 直捣黄龙 (30分) dijkstra + dfs
题目分析: 本题我有两种思路,一种是只依靠dijkstra算法,在dijkstra部分直接判断所有的情况,以局部最优解得到全局最优解,另一种是dijkstra + dfs,先计算出最短距离以及每个点的 ...
- BZOJ_4867_[Ynoi2017]舌尖上的由乃_分块+dfs序
BZOJ_4867_[Ynoi2017]舌尖上的由乃_分块+dfs序 Description 由乃为了吃到最传统最纯净的美食,决定亲自开垦一片菜园.现有一片空地,由乃已经规划n个地点准备种上蔬菜.最新 ...
- 【bzoj4016】[FJOI2014]最短路径树问题 堆优化Dijkstra+DFS树+树的点分治
题目描述 给一个包含n个点,m条边的无向连通图.从顶点1出发,往其余所有点分别走一次并返回. 往某一个点走时,选择总长度最短的路径走.若有多条长度最短的路径,则选择经过的顶点序列字典序最小的那条路径( ...
- PAT-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+DFS
PAT 1030 最短路最小边权 堆优化dijkstra+DFS 1030 Travel Plan (30 分) A traveler's map gives the distances betwee ...
- PAT-1018 Public Bike Management(dijkstra + dfs)
1018. Public Bike Management There is a public bike service in Hangzhou City which provides great co ...
- 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs
前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...
- BZOJ_4765_普通计算姬_分块+dfs序+树状数组
BZOJ_4765_普通计算姬_分块 Description "奋战三星期,造台计算机".小G响应号召,花了三小时造了台普通计算姬.普通计算姬比普通计算机要厉害一些 .普通计算机能 ...
- 1030 Travel Plan Dijkstra+dfs
和1018思路如出一辙,先求最短路径,再dfs遍历 #include <iostream> #include <cstdio> #include <vector> ...
- PAT1018 (dijkstra+dfs)
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
随机推荐
- XMU C语言程序设计实践(1)
题目: 任务1:英雄出世 炎热的夏天午后,小明正在百无聊赖地写c语言程序.忽然,电脑屏幕一阵抖动,浮现下面18×18个看似杂乱无章的数字: 32, 32, 32, 32, 32, 32, ...
- window下安装多个tomcat
解压该压缩包,生成3分tomcat 分别命名为 tomcat1,tomcat2,tomcat3 进入tomcat1/conf/目录,修改server.xml 进入tomcat1/bin目录,修改 se ...
- Package vim is not available, but is referred to by another package及我的vim配置
新安装的ubuntu,先安装vim,但是安装出现 Reading package lists... Done Building dependency tree Reading state inform ...
- VS2012变化的快捷键
VS2012变化的快捷键: 注释::VS2010是(Ctrl+E,C),VS2012是(Ctrl+K, Ctrl+C),实际操作,按住Ctrl键不放,先按K键,再按C键.相当于Ctrl+K加 Ctrl ...
- git不同分支局部代码合并 git cherry-pick
cherry-pick 可以局部代码合并. cherry-pick不仅可以用在不同分支之间,还可以用在同一个分支上. 比如说你在某一个向某个分支中添加了一个功能,后来处于某种原因把它给删除了,然而后来 ...
- java实现io读取数据
ServletInputStream inputStream = request.getInputStream(); BufferedReader br = new BufferedReader(ne ...
- ExtJS 4 MVC Viewport和card布局
http://ext4all.com/post/a-little-bit-strange-navigation 效果图: app/view/Viewport.js Ext.define('App.v ...
- 洛谷 P3731 [HAOI2017]新型城市化【最大流(二分图匹配)+tarjan】
我到底怎么建的图为啥要开这么大的数组啊?! 神题神题,本来以为图论出不出什么花来了. 首先要理解'团'的概念,简单来说就是无向图的一个完全子图,相关概念详见度娘. 所以关于团一般都是NP问题,只有二分 ...
- Quartz.Net实现的定时执行任务调度
在之前的文章<推荐一个简单.轻量.功能非常强大的C#/ASP.NET定时任务执行管理器组件–FluentScheduler>和<简单.轻量.功能非常强大的C#/ASP.NET定时调度 ...
- Java键盘输入的方法
转载:http://blog.csdn.net/u012249177/article/details/49586383 java输入的方法: import java.io.BufferedReader ...