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 ...
随机推荐
- Lightoj 1025 - The Specials Menu
区间dp /* *********************************************** Author :guanjun Created Time :2016/6/30 23:2 ...
- How to check HTML version of any website
http://howtocheckversion.com/check-html-version-website/ Check HTML version via W3C W3 Consortium ha ...
- YTU 2437: C++ 习题 比较大小-类模板
2437: C++ 习题 比较大小-类模板 时间限制: 1 Sec 内存限制: 128 MB 提交: 1144 解决: 805 题目描述 声明一个类模板,利用它分别实现两个整数.浮点数和字符的比较 ...
- bzoj1566 [NOI2009]管道取珠——DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1566 一眼看上去很懵... 但是答案可以转化成有两个人在同时取珠子,他们取出来一样的方案数: ...
- SDL2:封装媒体显示播放Csdl2
Github https://github.com/gongluck/SDL2-study/tree/master/Csdl2 Csdl2.h #ifndef __CSDL2_H__ #define ...
- Java Socket实战之一:单线程通信
转自:http://developer.51cto.com/art/201202/317543.htm 现在做Java直接使用Socket的情况是越来越少,因为有很多的选择可选,比如说可以用sprin ...
- (DP)51NOD 1183 编辑距离
编辑距离,又称Levenshtein距离(也叫做Edit Distance),是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除 ...
- JS 自写datapage.js 通用分页
var Page = function () { }; Page.prototype = { Loading: "<img src='/Content/Scripts/Data ...
- java启动参数二
非标准参数又称为扩展参数,其列表如下: -Xint 设置jvm以解释模式运行,所有的字节码将被直接执行,而不会编译成本地码. -Xbatch 关闭后台代码编译,强制在前台编译,编译完成之后才能进行代码 ...
- 区间DP UVA 10453 Make Palindrome
题目传送门 /* 题意:问最少插入多少个字符使得字符串变成回文串 区间DP:dp[i][j]表示[l, r]的字符串要成为回文需要插入几个字符串,那么dp[l][r] = dp[l+1][r-1]; ...