问题一:最短距离计数
例题:(洛谷P1608) https://www.luogu.com.cn/problem/P1608
坑点:去重边
#include<bits/stdc++.h>
#define ll long long
#define white 0
#define black 1
#define grey 2
#define endl '\n'
#define INF 0x3f3f3f3f3f
#define IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
const int maxn=2000+5;
int tot,head[maxn];
struct E{
int to,next,w;
}edge[maxn*maxn];
void add(int u,int v,int w){
edge[tot].to=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
}
ll d[maxn];ll color[maxn];
priority_queue<pair<ll,ll> >q;
ll n,m,s;int cnt[maxn];
void Dijkstra(ll s){
for(ll i=0;i<=n;i++) d[i]=INF,color[i]=white;
d[s]=0;cnt[s]=1;
q.push(make_pair(0,s));
color[s]=grey;
while(!q.empty()){
pair<ll,ll> f=q.top();
q.pop();
ll u=f.second;
color[u]=black;
if(d[u]<f.first*(-1)) continue;
for(ll i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(color[v]==black) continue;
if(d[v]==d[u]+edge[i].w){
cnt[v]+=cnt[u];
}
if(d[v]>d[u]+edge[i].w){
d[v]=d[u]+edge[i].w;
q.push(make_pair(d[v]*(-1),v));
color[v]=grey;
cnt[v]=cnt[u];
}
}
}
}
int mp[maxn][maxn];
int main(){
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
for(int i=1;i<=m;i++){
int u,v,w;scanf("%d%d%d",&u,&v,&w);
if(mp[u][v]==0||mp[u][v]>w){
add(u,v,w);
mp[u][v]=w;
}
}
Dijkstra(1);
if(d[n]!=INF) cout<<d[n]<<" "<<cnt[n]<<endl;
else puts("No answer");
}
最短距离最大顶点费用问题
#include<bits/stdc++.h>
#define ll long long
#define white 0
#define black 1
#define grey 2
#define endl '\n'
#define INF 0x3f3f3f3f3f
#define IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
const int maxn=2000+5;
int tot,head[maxn];
struct E{
int to,next,w;
}edge[maxn*maxn];
void add(int u,int v,int w){
edge[tot].to=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
}
ll d[maxn];ll color[maxn];
priority_queue<pair<ll,ll> >q;
ll n,m,s;int cnt[maxn],weight[maxn],w[maxn];
void Dijkstra(ll s){
for(ll i=0;i<=n;i++) d[i]=INF,color[i]=white;
d[s]=0;cnt[s]=1;w[s]=weight[s];
q.push(make_pair(0,s));
color[s]=grey;
while(!q.empty()){
pair<ll,ll> f=q.top();
q.pop();
ll u=f.second;
color[u]=black;
if(d[u]<f.first*(-1)) continue;
for(ll i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(color[v]==black) continue;
if(d[v]==d[u]+edge[i].w){
cnt[v]+=cnt[u];
if(w[v]<w[u]+weight[v]){
w[v]=w[u]+weight[v];
}
}
if(d[v]>d[u]+edge[i].w){
d[v]=d[u]+edge[i].w;
q.push(make_pair(d[v]*(-1),v));
color[v]=grey;
cnt[v]=cnt[u];
w[v]=w[u]+weight[v];
}
}
}
}
int mp[maxn][maxn];
int main(){
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
for(int i=1;i<=n;i++) scanf("%d",&weight[i]);
for(int i=1;i<=m;i++){
int u,v,w;scanf("%d%d%d",&u,&v,&w);
if(mp[u][v]==0||mp[u][v]>w){
add(u,v,w);
mp[u][v]=w;
}
}
Dijkstra(1);
}