http://acm.hdu.edu.cn/showproblem.php?pid=3790
题意:给出两个权值的图,距离和价值,距离优先于价值。求u、v间最短路径。
#include<bits/stdc++.h> using namespace std; typedef long long ll ; #define int ll #define mod 998244353 #define gcd(m,n) __gcd(m, n) #define rep(i , j , n) for(int i = j ; i <= n ; i++) #define red(i , n , j) for(int i = n ; i >= j ; i--) #define ME(x , y) memset(x , y , sizeof(x)) int lcm(int a , int b){return a*b/gcd(a,b);} //ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;} //int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;} //const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len} #define INF 0x3f3f3f3f #define PI acos(-1) #define pii pair<int,int> #define fi first #define se second #define lson l,mid,root<<1 #define rson mid+1,r,root<<1|1 #define pb push_back #define mp make_pair #define all(v) v.begin(),v.end() #define size(v) (int)(v.size()) #define cin(x) scanf("%lld" , &x); const int N = 1e4+9; const int maxn = 1e3+9; const double esp = 1e-6; int ma[maxn][maxn] , vis[maxn] , dis[maxn] , val[maxn]; int mi[maxn][maxn] ; int n , m ; void dijkstra(int u){ rep(i , 1 , n){ dis[i] = ma[u][i]; val[i] = mi[u][i]; } vis[u] = 1; rep(i , 1 , n-1){ int pos ; int mix = INF; int miv = INF; rep(j , 1 , n){ if(!vis[j] && mix > dis[j]){ mix = dis[j]; miv = val[j]; pos = j ; }else if(!vis[j] && mix == dis[j] && miv > val[j]){ miv = val[j]; pos = j ; } } vis[pos] = 1 ; rep(j , 1 , n){ if(!vis[j] && dis[j] > dis[pos] + ma[pos][j]){ dis[j] = dis[pos] + ma[pos][j]; val[j] = val[pos] + mi[pos][j]; }else if(!vis[j] && dis[j] == dis[pos] + ma[pos][j]){ val[j] = min(val[j] , val[pos] + mi[pos][j]); } } } } void init(){ fill(ma[0] , ma[0] + maxn*maxn , INF); fill(mi[0] , mi[0] + maxn*maxn , INF); ME(vis , 0); } void solve(){ init(); rep(i , 1 , m){ int u , v , w , va; scanf("%lld%lld%lld%lld" , &u , &v , &w , &va); if(ma[u][v] > w){//这里要注意可能出现重边,可能出现距离小的价值大,距离大的价值小,直接都取最小的话,没有对应起来出错。 ma[u][v] = ma[v][u] = w ; mi[u][v] = mi[v][u] = va; }else if(ma[u][v] == w){ mi[u][v] = va; } } int u , v ; scanf("%lld%lld" , &u , &v); dijkstra(u); cout << dis[v] << " " << val[v] << endl; } signed main() { while(~scanf("%lld%lld" , &n , &m) && n+m) solve(); }
堆优化:
#include<bits/stdc++.h> using namespace std; typedef long long ll ; #define int ll #define mod 998244353 #define gcd(m,n) __gcd(m, n) #define rep(i , j , n) for(int i = j ; i <= n ; i++) #define red(i , n , j) for(int i = n ; i >= j ; i--) #define ME(x , y) memset(x , y , sizeof(x)) int lcm(int a , int b){return a*b/gcd(a,b);} //ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;} //int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;} //const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len} #define INF 0x3f3f3f3f #define PI acos(-1) #define pii pair<int,int> #define fi first #define se second #define lson l,mid,root<<1 #define rson mid+1,r,root<<1|1 #define pb push_back #define mp make_pair #define all(v) v.begin(),v.end() #define size(v) (int)(v.size()) #define cin(x) scanf("%lld" , &x); const int N = 1e5+9; const int maxn = 1e3+9; const double esp = 1e-6; int head[maxn] , tol ; int dis[maxn] , vis[maxn] , val[maxn]; int n , m ; struct Graph{ int v , w , p , next; }g[N<<1]; void add(int u , int v , int w , int p){ g[++tol] = {v , w , p , head[u]}; head[u] = tol; } struct Edge{ int v , w , p; bool operator < (const Edge &e) const{ if(w > e.w){ return true; }else if(w < e.w){ return false; }else{ return p > e.p; } } Edge(int _v , int _w , int _p){ v = _v , w = _w , p = _p; } Edge(){} }; void dijkstra(int u){ fill(dis , dis+maxn , INF); fill(val , val+maxn , INF); dis[u] = 0 ; val[u] = 0 ; priority_queue<Edge>q; q.push(Edge(u , dis[u] , val[u])); Edge now; while(!q.empty()){ now = q.top() ; q.pop(); vis[now.v] = 1 ; for(int i = head[now.v] ; i ; i = g[i].next){ int v = g[i].v , w = g[i].w , p = g[i].p; if(!vis[v] && dis[v] > dis[now.v] + w){ dis[v] = dis[now.v] + w ; val[v] = val[now.v] + p ; q.push(Edge(v , dis[v] , val[v])); }else if(!vis[v] && dis[v] == dis[now.v] + w && val[v] > val[now.v] + p){ val[v] = val[now.v] + p; q.push(Edge(v , dis[v] , val[v])); } } } } void init(){ ME(vis , 0); ME(head , 0); tol = 0 ; } void solve(){ init(); rep(i , 1 , m){ int u , v , w , p ; scanf("%lld%lld%lld%lld" , &u , &v , &w , &p); add(u , v , w , p); add(v , u , w , p); } int u , v ; scanf("%lld%lld" , &u , &v); dijkstra(u); cout << dis[v] << " " << val[v] << endl; } signed main() { while(~scanf("%lld%lld" , &n , &m) && n+m) solve(); }