1003 Emergency (25 分)(求最短路径)
给出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 分)(求最短路径)的更多相关文章
- 1003 Emergency (25分) 求最短路径的数量
1003 Emergency (25分) As an emergency rescue team leader of a city, you are given a special map of ...
- 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 ...
- 1003 Emergency (25分)
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
- 【PAT甲级】1003 Emergency (25 分)(SPFA,DFS)
题意:n个点,m条双向边,每条边给出通过用时,每个点给出点上的人数,给出起点终点,求不同的最短路的数量以及最短路上最多能通过多少人.(N<=500) AAAAAccepted code: #in ...
- PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 解题报告 1003. Emergency (25)
1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special map of yo ...
- PAT 1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- 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 ...
- PAT 甲级 1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- 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 ...
随机推荐
- android中cursor对象的使用
cursor对象是使用行来存储数据的,你要使用它获得数据,就必须知道每一列的数据名称以及他的数据类型才能获得对象数据 常见的方法: .close()关闭资源:记住,所有的资源对象使用完成后都要主动关闭 ...
- 课时15.DTD文档声明下(了解)
W3C的官方网站是W3School,我们可以去官方网站查询DTD文档声明. HTML4.01 Strict 非常严谨的 如果你写了这个DTD文档声明,你就不能写如下样式: <fon ...
- C#添加二维码带加密带logo
#region 生成QR码,加密与logo在此处修改 public static void CreateQr(string strQrContent, DataTable myTable) { Qr ...
- mix-blend-mode 混合模式 background-blend-mode 背景混合模式 isolation:isolate 隔离
css3 mix-blend-mode 混合模式 该属性不仅可以作用于HTML,还可以作用于SVG 兼容性: IE 8~11 Edge 12~14 Firefox 41~47 chrome 45~51 ...
- C++练习--创建Boat类和Car类(含友元)
/* 定义Boat与Car两个类,二者都有weight属性, 定义二者的一个友元函数totalWeight()为外部函数, 计算二者的重量和. */ #include<iostream> ...
- HDU1159(LCS)
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> us ...
- P1330 封锁阳光大学 DFS+染色
题目链接:https://www.luogu.org/problemnew/show/P1330 这个题有意思,如果能想到染色,就会很简单,但若想不到就很麻烦 要想把一条边封锁,就必须且只能占据这条边 ...
- chromium之tracked
//------------------------------------------------------------------------------ // Tracked is the b ...
- 构建高可靠hadoop集群之4-保全模式
本文主要翻译自http://hadoop.apache.org/docs/r2.8.0/hadoop-project-dist/hadoop-common/SecureMode.html 译注:之所以 ...
- vue服务端渲染添加缓存
缓存 虽然 Vue 的服务器端渲染(SSR)相当快速,但是由于创建组件实例和虚拟 DOM 节点的开销,无法与纯基于字符串拼接(pure string-based)的模板的性能相当.在 SSR 性能至关 ...