而每个点的花费为 根结点到该点的距离(边权unit)* 该点的重量。
思路:求出各点的最短路径 再与点的重量相乘之和; 注意两点我觉得,inf 要定义足够大,10个9吧,还有,用memset
初始化会严重超时,不用竟然才600多MS 。。。。这个我无语了。。。。
#include <stdio.h>
#include <string.h>
#define VM 50005
#define EM 100005
#define inf 9999999999
int head[VM],ep;
int weigt[VM];
struct dege
{
v,w,next;
} e[EM];
void addedge(int cu,int cv,int cw)
{
cv;
cw;
head[cu];
ep;
}
__int64 spfa (int n)
{
vis[VM],queue[VM],cnt[VM];
dis[VM];
(vis,0,sizeof(vis));
(cnt,0,sizeof(cnt));
(dis,0x3f,sizeof(dis));
1; i <= n; i ++)
cnt[i] = 0;
vis[i] = 0;
dis[i] = inf;
0;
-1,rear = 0;
queue[rear++] = 1;
1;
1;
!= rear)
front = (front+1)%(n+1);
int u = queue[front];
vis[u] = 0;
for (int i = head[u]; i != -1; i = e[i].next)
{
int v = e[i].v;
if (dis[v] > dis[u] + e[i].w)
{
dis[v] = dis[u] + e[i].w;
if (!vis[v])
{
vis[v] = 1;
queue[rear] = v;