您的位置:首页 > 其它

HDU - 3790最短路径问题经典模板题

2015-08-04 20:56 387 查看
HDU - 3790

最短路径问题

Time Limit: 1000MSMemory Limit: 32768KB64bit IO Format: %I64d & %I64u
Submit Status

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


/*
Author: 2486
Memory: 3924 KB		Time: 296 MS
Language: G++		Result: Accepted
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 1000 + 5;
int d[maxn],v[maxn];
bool vis[maxn][maxn];
int n,m,s,t;
typedef pair<int,int>P;

struct edge {
int to,cost,val;
edge(int to,int cost,int val):to(to),cost(cost),val(val) {}
};
vector<edge>G[maxn];
void init() {
for(int i = 1; i <= n; i ++)G[i].clear();
memset(vis,false,sizeof(vis));
}
void Dijkstra(int s) {
priority_queue<P,vector<P>,greater<P> >que;
memset(d,0x3f,sizeof(d));
memset(v,0,sizeof(v));
d[s] = 0;
v[s] = 0;
que.push(P(0,s));
while(!que.empty()) {
P p = que.top();
que.pop();
int vs = p.second;
if(d[vs] < p.first)continue;
for(int i = 0; i < G[vs].size(); i ++) {
edge e = G[vs][i];
if(d[e.to] > d[vs] + e.cost||(d[e.to] == d[vs] + e.cost && v[e.to] > v[vs] + e.val)) {
d[e.to]=d[vs] + e.cost;
v[e.to]=v[vs]+e.val;
que.push(P(d[e.to],e.to));
}
}
}
printf("%d %d\n",d[t],v[t]);
}
int main() {
int a,b,d,p;
while(~scanf("%d%d",&n,&m),n&&m) {
init();
for(int i = 0; i < m; i ++) {
scanf("%d%d%d%d",&a,&b,&d,&p);
if(vis[a][b])continue;
G[a].push_back(edge(b,d,p));
G[b].push_back(edge(a,d,p));
}
scanf("%d%d",&s,&t);
Dijkstra(s);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: