poj3013

spfa,说是边的价值=承重*单位价,实际上就是把到达每个结点所需经过的边的单位价之和乘以结点重量之积加起来即可。所以我们就是要让到达每个结点经过的边的单位价格之和最小。这就变成了最短路问题。

这道题用栈超时,用队列能过。所以以后用spfa还是用队列的好。

View Code
#include <iostream>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
using namespace std;

#define maxn 150005

struct Edge
{
int v, w, next;
} edge[maxn];

int v, e, ncount;
long long weight[maxn];
int head[maxn];
long long stk[maxn], f, r, xtime[maxn];
bool instk[maxn];

void input()
{
memset(head,
-1, sizeof(head));
ncount
= 0;
scanf(
"%d%d", &v, &e);
for (int i = 0; i < v; i++)
scanf(
"%I64d", &weight[i]);
for (int i = 0; i < e; i++)
{
int a, b, c;
scanf(
"%d%d%d", &a, &b, &c);
a
--;
b
--;
edge[ncount].next
= head[a];
head[a]
= ncount;
edge[ncount].v
= b;
edge[ncount].w
= c;
ncount
++;
edge[ncount].next
= head[b];
head[b]
= ncount;
edge[ncount].v
= a;
edge[ncount].w
= c;
ncount
++;
}
}

void spfa()
{
memset(xtime,
-1, sizeof(xtime));
memset(instk,
0, sizeof(instk));
f
= 0;
r
= 0;
stk[r
++] = 0;
instk[
0] = true;
xtime[
0] = 0;
while (f != r)
{
int temp = stk[f++];
instk[temp]
= false;
if (f == maxn)
f
= 0;
for (int i = head[temp]; i != -1; i = edge[i].next)
{
if (xtime[edge[i].v] > xtime[temp] + edge[i].w || xtime[edge[i].v] == -1)
{
xtime[edge[i].v]
= xtime[temp] + edge[i].w;
if (!instk[edge[i].v])
{
stk[r
++] = edge[i].v;
instk[edge[i].v]
= true;
if (r == maxn)
r
= 0;
}
}
}
}
}

void output()
{
long long sum = 0;
for (int i = 0; i < v; i++)
{
if (xtime[i] == -1)
{
printf(
"No Answer\n");
return;
}
sum
+= weight[i] * xtime[i];
}
printf(
"%I64d\n", sum);
}

int main()
{
//freopen("D:\\t.txt", "r", stdin);
int t;
scanf(
"%d", &t);
while (t--)
{
input();
spfa();
output();
}
return 0;
}
posted @ 2011-03-01 10:34  金海峰  阅读(688)  评论(0编辑  收藏  举报