给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。
Input
输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t;起点s,终点。n和m为0时输入结束。
(1<n<=1000, 0<m<100000, s != t)
Output
输出 一行有两个数, 最短距离及其花费。
Sample Input
3 2
1 2 5 6
2 3 4 5
1 3
0 0
Sample Output
9 11
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#define ll long long
#define inf 1e9
#define N 1010
using namespace std;
struct edge{
int to,cost,pr;//顶点,距离,花费
bool operator<(const edge n1)const{
if(n1.cost==cost)return n1.pr<pr;
return n1.cost<cost;
};
};
int n,m,s,t;
int main(){
while(scanf("%d %d",&n,&m)!=EOF&&n+m){
vector<edge>v[N];
int a,b,c,f;
while(m--){
scanf("%d %d %d %d",&a,&b,&c,&f);
edge e;
e.to=b;
e.cost=c;
e.pr=f;
v[a].push_back(e);
e.to=a;
v[b].push_back(e);
}
scanf("%d %d",&s,&t);
int vis[N]={0};
priority_queue<edge>que;
edge x;
x.to=s;x.cost=0;x.pr=0;
que.push(x);
while(!que.empty()){
x=que.top();que.pop();
vis[x.to]=1;
if(x.to==t)break;
for(int i=0,j=v[x.to].size();i<j;i++){
edge e;
e.to=v[x.to][i].to;
if(vis[e.to])continue;
e.cost=x.cost+v[x.to][i].cost;
e.pr=x.pr+v[x.to][i].pr;
que.push(e);
}
}
printf("%d %d\n",x.cost,x.pr);
}
return 0;
}