标签:最短路
3 2 1 2 5 6 2 3 4 5 1 3 0 0
9 11
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1010;
const int M = 100010;
const int inf = 0x3f3f3f3f;
int dist[N];
int cost[N];
int head[N];
int tot, n, m;
struct node
{
int weight;
int cost;
int next;
int to;
}edge[M << 1];
void addedge(int from, int to, int weight, int p)
{
edge[tot].weight = weight;
edge[tot].cost = p;
edge[tot].to = to;
edge[tot].next = head[from];
head[from] = tot++;
}
void spfa(int v0)
{
memset (dist, inf, sizeof(dist));
memset (cost, inf, sizeof(cost));
dist[v0] = 0;
cost[v0] = 0;
queue <int> qu;
while (!qu.empty())
{
qu.pop();
}
qu.push(v0);
while (!qu.empty())
{
int u = qu.front();
qu.pop();
for (int i = head[u]; ~i; i = edge[i].next)
{
int v = edge[i].to;
// printf("%d %d\n", dist[v], dist[u] + edge[i].weight);
if (dist[v] > dist[u] + edge[i].weight)
{
dist[v] = dist[u] + edge[i].weight;
cost[v] = cost[u] + edge[i].cost;
qu.push(v);
}
else if(dist[v] == dist[u] + edge[i].weight && cost[v] > cost[u] + edge[i].cost)
{
dist[v] = dist[u] + edge[i].weight;
cost[v] = cost[u] + edge[i].cost;
qu.push(v);
}
}
}
}
int main()
{
int u, v, w, p;
while (~scanf("%d%d", &n, &m))
{
if (!n && !m)
{
break;
}
memset (head, -1, sizeof(head));
tot = 0;
for (int i = 0; i < m; ++i)
{
scanf("%d%d%d%d", &u, &v, &w, &p);
addedge(u, v, w, p);
addedge(v, u, w, p);
}
scanf("%d%d", &u, &v);
spfa(u);
printf("%d %d\n", dist[v], cost[v]);
}
}标签:最短路
原文地址:http://blog.csdn.net/guard_mine/article/details/41346095