给出N个城市,m条无向边。每个城市中都有一定数目的救援小组,所有边的边权已知。现在给出起点和终点,求从起点到终点的最短路径条数及最短经上的救缓小组数目只和。如果有多条最短路径,则输出数目只和最大的

Dijkstra 做法

#include<bits/stdc++.h>
using namespace std;
int n,m,s,u;
const int N=;
const int inf=0x3f3f3f3f;
int mp[N][N];
int dis[N];
bool vis[N];
int value[N];
int num[N];
int w[N];
void Dijkstra()
{
fill(vis,vis+N,false);
fill(dis,dis+N,inf);
fill(w,w+N,);
fill(num,num+N,);
num[s]=;//赋值
w[s]=value[s];//赋值
for(int i=;i<n;i++) dis[i]=mp[s][i];
dis[s]=;
for(int i=;i<n-;i++){
int u=-;
int minn=inf;
for(int j=;j<n;j++){
if(!vis[j]&&dis[j]<minn){
u=j;
minn=dis[j];
}
}
if(u==-) return;
vis[u]=true;
for(int j=;j<n;j++){
if(!vis[j]&&dis[u]+mp[u][j]<=dis[j]){
if(mp[u][j]+dis[u]<dis[j]){
dis[j]=mp[u][j]+dis[u];
num[j]=num[u];
w[j]=w[u]+value[j];
}
else{
num[j]+=num[u];
if(w[u]+value[j]>w[j]){
w[j]=w[u]+value[j];
}
}
}
} }
}
int main()
{
scanf("%d %d %d %d",&n,&m,&s,&u);
for(int i=;i<n;i++) scanf("%d",&value[i]);
memset(mp,inf,sizeof(mp));
while(m--){
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
mp[a][b]=mp[b][a]=c;
}
Dijkstra();
printf("%d %d",num[u],w[u]); return ;
}

spfa做法

#include<bits/stdc++.h>

using namespace std;
int n,m,s,v;
struct node
{
int to;
int dis;
node(int _to=,int _dis=):to(_to),dis(_dis){}
};
const int N=;
int dis[N];
bool vis[N];
int w[N];
int num[N];
int value[N];
vector<node>mp[N];
set<int>st[N];
const int inf=0x3f3f3f3f;
void spfa()
{
fill(dis,dis+N,inf);
fill(vis,vis+N,false);
fill(w,w+N,);
w[s]=value[s];
num[s]=;
queue<int>Q;
Q.push(s);
vis[s]=true;
dis[s]=;
while(!Q.empty()){
int u=Q.front();
Q.pop();
vis[u]=false;
for(int i=;i<mp[u].size();i++){
int to=mp[u][i].to;
int diss=mp[u][i].dis;
if(diss+dis[u]<dis[to]){
dis[to]=diss+dis[u];
w[to]=w[u]+value[to];
num[to]=num[u];
st[to].clear();
st[to].insert(u);
if(!vis[to]){
Q.push(to);
vis[to]=true;
}
}
else if(diss+dis[u]==dis[to]){
if(w[to]<w[u]+value[to]){
w[to]=w[u]+value[to];
}
st[to].insert(u);
num[to]=;//因为spfa会重复到一个点 所以可能重复的边
for(set<int>::iterator it=st[to].begin();it!=st[to].end();++it){
num[to]+=num[*it];
}
if(!vis[to]){
Q.push(to);
vis[to]=true;
}
}
}
}
}
int main()
{
scanf("%d %d %d %d",&n,&m,&s,&v);
for(int i=;i<n;i++) scanf("%d",&value[i]);
for(int i=;i<n;i++) mp[i].clear();
while(m--){
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
mp[a].push_back(node(b,c));
mp[b].push_back(node(a,c));
}
spfa();
printf("%d %d\n",num[v],w[v]); return ;
}

1003 Emergency (25 分)(求最短路径)的更多相关文章

  1. 1003 Emergency (25分) 求最短路径的数量

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

  2. PAT 1003 Emergency (25分)

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

  3. 1003 Emergency (25分)

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

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

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

  5. PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

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

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

  7. PAT 1003. Emergency (25)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  8. 1003 Emergency (25)(25 point(s))

    problem 1003 Emergency (25)(25 point(s)) As an emergency rescue team leader of a city, you are given ...

  9. PAT 甲级 1003. Emergency (25)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  10. PAT 甲级1003 Emergency (25)(25 分)(Dikjstra,也可以自己到自己!)

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

随机推荐

  1. Android onMeasure 方法的测量规范MeasureSpec

    一个MeasureSpec封装了父布局传递给子布局的布局要求,每个MeasureSpec代表了一组宽度和高度的要求.一个MeasureSpec由大小和模式组成.它有三种模式:UNSPECIFIED(未 ...

  2. 使用Mosh,本地Mac locale与Remote Debian locale不一致的问题

    事实上, 你并不需要安装语言包, 只需安装中文字体并将/etc/locale.gen 中zh_CN.UTF-8 前的注释符号去掉, 执行sudo locale-gen 然后重启即可.

  3. EF6.0 对于数据库优 模式 新加功能

    EF6.0相对于5.0新加了很多功能.先看看两个模式的一些特点. 数据库优先(设计者)和代码优先两者的特点: 连接弹性 异步查询和保存 基于代码的配置 数据库命令记录 数据库命令截取 依赖决议 DbS ...

  4. 开发工具--Eclipse使用及常见问题解决

    怎么查询Eclipse版本号: 方法一: 方法二: Eclipse安装目录下面找到readme文件夹,里边有个网页打开就可以看到当前版本; Eclipse汉化改为英文: Eclipse Mybatis ...

  5. 配置JAVA_HOME踩得坑 。。

    配置JAVA_HOME 的时候这里不能有空格哦 ,还以为什么呢...

  6. stl之std::remove_copy

    template <class InputIterator, class OutputIterator, class T> OutputIterator remove_copy (Inpu ...

  7. iOS | CAShapeLayer转场动画

    什么也不说了,作为一名乐于分享技术的小开发,直接先上个样式最为直观贴切,有需要的朋友可以直接拿过去用. 需要demo请点击这里 :github 在这个demo中,核心为选用画布CAShapeLayer ...

  8. 请对比 Exception 和 Error,另外,运行时异常与一般异常有什么区别?

    error指的是不可预料的错误,可能会导致程序宕机:而exception指的是在程序运行中可以预见的异常,而异常分为检查异常与一般异常,检查异常需要在程序中显示捕获并处理,一般异常可以通过程序编码来进 ...

  9. POJ 1113--Wall(计算凸包)

    Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 40363 Accepted: 13754 Description On ...

  10. Failed to introspect bean class [org.springframework.orm.hibernate5.LocalSessionFactoryBean] for lookup method metadata: could not find class that it depends on; nested exception is java.lang.NoClass

    依赖引入  错误可能版本 不对 Failed to introspect bean class [org.springframework.orm.hibernate5.LocalSessionFact ...