题意:给你一些高速公路,问从起点到终点的最短距离同时保证距离相同的时候花费最小
思路:其实还是最短路虽然有两个维度,但是有明确的优先级,一个个比就好啦
代码:
#include<cstdio>
#include<cstring>
const int maxn=2005;
const int INF=1000000005;
struct PP
{
int t,w,c;
int next;
}edge[maxn];
int tot,head[maxn];
void add_edge(int s,int t,int w,int c){
edge[tot].t=t;
edge[tot].w=w;
edge[tot].c=c;
edge[tot].next=head[s];
head[s]=tot++;
}
int n,m;
int dis[maxn],cost[maxn],p[maxn];
bool mark[maxn];
void Dij(int s)
{
int i,j;
memset(mark,false,sizeof(mark));
for(i=0;i<n;i++)
{
dis[i]=INF;
cost[i]=INF;
}
dis[s]=0;
cost[s]=0;
for(i=0;i<n;i++)
{
int t=-1,Min=INF,Minc=INF;
for(j=0;j<n;j++) if(!mark[j])
{
if(dis[j]<Min)
{
Min=dis[j];
Minc=cost[j];
t=j;
}
else if(dis[j]==Min&&cost[j]<Minc)
{
Minc=cost[j];
t=j;
}
}
if(t==-1)
return ;
mark[t]=1;
for(j=head[t];j!=-1;j=edge[j].next) if(!mark[edge[j].t])
{
int v=edge[j].t;
if(dis[t]+edge[j].w<dis[v])
{
dis[v]=dis[t]+edge[j].w;
cost[v]=cost[t]+edge[j].c;
p[v]=t;
}
else if(dis[t]+edge[j].w==dis[v]&&cost[t]+edge[j].c<cost[v])
{
cost[v]=cost[t]+edge[j].c;
p[v]=t;
}
}
}
}
void dfs(int s,int n)
{
if(n==s) {
printf("%d ",s);
return ;
}
dfs(s,p[n]);
printf("%d ",n);
}
int main()
{
int i,j,s,t;
scanf("%d%d%d%d",&n,&m,&s,&t);
tot=0;
memset(head,-1,sizeof(head));
for(i=0;i<m;i++)
{
int u,v,w,c;
scanf("%d%d%d%d",&u,&v,&w,&c);
add_edge(u,v,w,c);
add_edge(v,u,w,c);
}
Dij(s);
dfs(s,t);
printf("%d %d\n",dis[t],cost[t]);
return 0;
}