【图论】最短路中(最短距离计数+最短距离最大顶点费用)

问题一:最短距离计数

例题:(洛谷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);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值