您的位置:首页 > 其它

hdoj3790_最短路径问题(最短路径)

2015-05-24 12:28 218 查看
#include<cstdio>
#include<iostream>
#include<vector>
#include<string.h>
using namespace std;

struct edge{
int npos, dis, cost;
};

vector<edge> edges[2010];
pair<int, int> res[2010];
bool used[2010];

void kruskal(int s, int t,int n)
{
memset(used, false, sizeof(used));
used[s] = true;
fill(res, res + 2010, make_pair(999999,999999));
for (int i = 0; i < edges[s].size(); i++)
if( (res[edges[s][i].npos].first > edges[s][i].dis) ||
(res[edges[s][i].npos].first == edges[s][i].dis)&&(res[edges[s][i].npos].second>edges[s][i].cost))
{
res[edges[s][i].npos].first = edges[s][i].dis;
res[edges[s][i].npos].second = edges[s][i].cost;
}
while (1)
{
int npos, tdis= 999999, tcost = 999999;
for (int i = 0; i < n;i++)
if (!used[i])
if ((res[i].first < tdis) || (res[i].first == tdis) && (res[i].second < tcost))
{
npos = i;
tdis = res[i].first;
tcost = res[i].second;
}
if (npos == t)
{
printf("%d %d\n", tdis, tcost);
return;
}
used[npos] = true;
for (int i = 0; i < edges[npos].size(); i++)
{
int tpos = edges[npos][i].npos;
if (!used[tpos])
if ((res[tpos].first > tdis + edges[npos][i].dis) || (res[tpos].first == tdis + edges[npos][i].dis) && (res[tpos].second > tcost + edges[npos][i].cost))
{
res[tpos].first = tdis + edges[npos][i].dis;
res[tpos].second = tcost + edges[npos][i].cost;
}
}
}
}

int main()
{
int n, m;
while (scanf("%d %d",&n,&m)&&n)
{
for (int i = 0; i < n; i++)
edges[i].clear();
for (int i = 0; i < m; i++)
{
int a, b, d, p;
scanf("%d %d %d %d", &a, &b, &d, &p);
edges[a - 1].push_back(edge{b-1,d,p});
edges[b - 1].push_back(edge{a-1,d,p});
}
int s, t;
scanf("%d %d", &s, &t);
kruskal(s-1,t-1,n);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: