最短路+dfs

先找出可能在最短路上的边,这些边会构成一个DAG,然后在这个DAG上dfs一次就可以得到两个答案了。

也可以对DAG进行拓扑排序,然后DP求解。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std; const int INF=0x7FFFFFFF;
const int maxn=;
int n,m,C1,C2,tot,ans1,ans2;
int val[maxn];
struct Edge
{
int u,v,L;
}e[];
int f[];
vector<int>g[maxn];
int dis[][maxn];
int h[maxn]; void init()
{
tot=;
for(int i=;i<=;i++) g[i].clear();
memset(h,,sizeof h);
} void add(int a,int b,int c)
{
e[tot].u=a,e[tot].v=b,e[tot].L=c;
g[a].push_back(tot);
tot++;
} void read()
{
scanf("%d%d%d%d",&n,&m,&C1,&C2);
for(int i=;i<n;i++) scanf("%d",&val[i]);
for(int i=;i<=m;i++)
{
int u,v,L; scanf("%d%d%d",&u,&v,&L);
add(u,v,L);
add(v,u,L);
}
} void SPFA(int f,int st)
{
for(int i=;i<=;i++) dis[f][i]=INF;
dis[f][st]=;
int flag[maxn]; memset(flag,,sizeof flag);
queue<int>Q; Q.push(st); flag[st]=;
while(!Q.empty())
{
int head= Q.front(); Q.pop(); flag[head]=;
for(int i=;i<g[head].size();i++)
{
int id=g[head][i];
if(dis[f][head]+e[id].L<dis[f][e[id].v])
{
dis[f][e[id].v]=dis[f][head]+e[id].L;
if(flag[e[id].v]==)
{
Q.push(e[id].v);
flag[e[id].v]=;
}
}
}
}
} void dfs(int x,int v)
{
if(x==C2)
{
ans1++; ans2=max(ans2,v);
return ;
} for(int i=;i<g[x].size();i++)
{
int id=g[x][i];
if(f[id]==) continue;
dfs(e[id].v,v+val[e[id].v]);
}
} void work()
{
int len=dis[][C2]; for(int i=;i<tot;i++)
if(dis[][e[i].u]+e[i].L+dis[][e[i].v]==len)
f[i]=; ans1=; dfs(C1,val[C1]);
printf("%d %d\n",ans1,ans2);
} int main()
{
init();
read();
SPFA(,C1);
SPFA(,C2);
work();
return ;
}

PAT (Advanced Level) 1003. Emergency (25)的更多相关文章

  1. PAT (Advanced level) 1003. Emergency (25) Dijkstra

    As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...

  2. PAT 解题报告 1003. Emergency (25)

    1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special map of yo ...

  3. PTA (Advanced Level) 1003 Emergency

    Emergency As an emergency rescue team leader of a city, you are given a special map of your country. ...

  4. PAT (Advanced Level) 1078. Hashing (25)

    二次探测法.表示第一次听说这东西... #include<cstdio> #include<cstring> #include<cmath> #include< ...

  5. PAT (Advanced Level) 1070. Mooncake (25)

    简单贪心.先买性价比高的. #include<cstdio> #include<cstring> #include<cmath> #include<vecto ...

  6. PAT (Advanced Level) 1029. Median (25)

    scanf读入居然会超时...用了一下输入挂才AC... #include<cstdio> #include<cstring> #include<cmath> #i ...

  7. PAT (Advanced Level) 1010. Radix (25)

    撸完这题,感觉被掏空. 由于进制可能大的飞起..所以需要开longlong存,答案可以二分得到. 进制很大,导致转换成10进制的时候可能爆long long,在二分的时候,如果溢出了,那么上界=mid ...

  8. PAT (Advanced Level) 1032. Sharing (25)

    简单题,不过数据中好像存在有环的链表...... #include<iostream> #include<cstring> #include<cmath> #inc ...

  9. 【PAT甲级】1003 Emergency (25 分)(SPFA,DFS)

    题意:n个点,m条双向边,每条边给出通过用时,每个点给出点上的人数,给出起点终点,求不同的最短路的数量以及最短路上最多能通过多少人.(N<=500) AAAAAccepted code: #in ...

随机推荐

  1. 天津工业大学CST专业培养方案

    第二学期 网页设计基础 计算机前沿技术 大学生心理健康与人生发展 高级语言程序设计II 思想道德修养与法律基础 职业生涯规划 大学英语二级(A) 大学物理(理三1) 线性代数 高等数学(理一2) 体育 ...

  2. spring security 3中的10个典型用法小结

    spring security 3比较庞大,但功能很强,下面小结下spring security 3中值得 注意的10个典型用法 1)多个authentication-provide可以同时使用 &l ...

  3. PHP fpm优化【转】

    在优化PHP的进程数的时候我们首先要了解我们服务器执行一个php使用的内存 1: 查询一个php占用的内存方法 pmap $(pgrep php-fpm | head -1) 我这里查询到的是 000 ...

  4. mysql 连接两列

    以下划线符号,连接两列,作为查询结果: SELECT CONCAT(col_1,'_',col_2) FROM yourtable

  5. 响应式web设计的优化

    在响应式网页的设计过程中,其中也会涉及到一些用户体验方面的问题和弊端,需要进一步的优化. (1)减轻Javascript库负载 对于移动端来说,jQuery表现的太过厚重,而现在针对移动端的状态来说, ...

  6. 【0-1 背包模板】 poj 3624

    先看个未经优化的二维空间dp: #include <iostream> #include <cstdio> #include <cmath> #include &l ...

  7. WPF InkCanvas 画图 基础使用教程

    大家好,由于很多原因,我有很长一段时间没有在 CSDN 上分享我的学习成果了,如今终于可以回归分享之路了. 之前在做一个项目的时候,想在一个区域里绘制自己的图形,于是上网搜索资料,无意中找到了 Ink ...

  8. [转]SSL协议详解

    背景介绍    最近在看<密码学与网络安全>相关的书籍,这篇文章主要详细介绍一下著名的网络安全协议SSL. 在开始SSl介绍之前,先给大家介绍几个密码学的概念和相关的知识.     1.密 ...

  9. java 输入、输出流

  10. android 界面悬浮框实现

    // 定义浮动窗口布局 private View mFloatLayout; // 定义浮动窗口布局对象 private WindowManager.LayoutParams wmParams; // ...