标签:
Input
Output
Sample Input
3 2 1 2 5 6 2 3 4 5 1 3 0 0
Sample Output
9 11
两点最短距离(无负环)用dijstra算法 这题不同在于有两个权值。
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> #include <stack> #include <vector> #include <map> #include <cmath> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define LL long long using namespace std; const int INF=0x3f3f3f3f; const int MX=1000+10; int n,m,Map[MX][MX],price[MX][MX],dis[MX],pre[MX]; void init(){ for (int i=1;i<=n;i++) for (int j=1;j<=n;j++){ Map[i][j]=INF; price[i][j]=INF; } } void dijkstra(int v0){ bool vis[MX]; for (int i=1;i<=n;i++){ dis[i]=Map[v0][i]; pre[i]=price[v0][i]; vis[i]=false; } dis[v0]=0; vis[v0]=true; for (int i=1;i<=n;i++){ int tmp=INF,P=INF,u; for (int j=1;j<=n;j++) if (!vis[j]&&dis[j]<tmp){ u=j; tmp=dis[j]; } vis[u]=true; for (int j=1;j<=n;j++) if (!vis[j]){ if (dis[u]+Map[u][j]<dis[j]){ dis[j]=dis[u]+Map[u][j]; pre[j]=pre[u]+price[u][j]; }else if (dis[u]+Map[u][j]==dis[j]&&pre[u]+price[u][j]<pre[j]) pre[j]=pre[u]+price[u][j]; } } } int main(){ while (~scanf ("%d %d",&n,&m)){ if (n==0&&m==0) break; init(); while (m--){ int u,v,w,p; scanf ("%d %d %d %d",&u,&v,&w,&p); if (Map[u][v]>w){ Map[u][v]=w; Map[v][u]=w; price[u][v]=p; price[v][u]=p; }else if (Map[u][v]==w&&price[u][v]>p){ price[u][v]=p; price[v][u]=p; } } int s,t; scanf ("%d %d",&s,&t); dijkstra(s); printf ("%d %d\n",dis[t],pre[t]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/yusize/p/5750800.html