题目传送门
Problem Description
给你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 <queue>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define m(a,b) memset(a,b,sizeof a)
#define mak(a,b) make_pair(a,b)
#define sdd(a,b) scanf("%d%d",&a,&b)
typedef long long ll;
using namespace std;
const int N=1005;
const int M=100000+5;
const int INF=0x3f3f3f3f;
int head[N],d[N],c[N];
int tot,n,m,s,t;
struct Edge{int to,len,nex,val;}edge[M*2];
void add(int from,int to,int len,int val)
{
edge[++tot]=(Edge){to,len,head[from],val};head[from]=tot;
edge[++tot]=(Edge){from,len,head[to],val};head[to]=tot;
}
priority_queue<pair<int,int> >q;
void dij()
{
m(d,INF);
d[s]=0,c[s]=0;
q.push(mak(0,s));
while(!q.empty())
{
int x=q.top().second;
q.pop();
for(int i=head[x];i;i=edge[i].nex)
{
int y=edge[i].to,l=edge[i].len,val=edge[i].val;
if(d[y]>d[x]+l)
{
d[y]=d[x]+l;
c[y]=c[x]+val;
q.push(mak(-d[y],y));
}
if(d[y]==d[x]+l&&c[y]>c[x]+val)
c[y]=c[x]+val;
}
}
}
int main()
{
int a,b,aa,bb;
while(~sdd(n,m),n||m)
{
m(head,0);
m(c,0);
tot=0;
while(!q.empty())
q.pop();
while(m--)
{
scanf("%d%d%d%d",&a,&b,&aa,&bb);
add(a,b,aa,bb);
}
sdd(s,t);
dij();
printf("%d %d\n",d[t],c[t]);
}
}