您的位置:首页 > 其它

zoj 2770 Burn the Linked Camp

2013-12-30 20:48 363 查看
Bellman_Ford实现:点击打开/article/10999315.html

差分约束系统的SPFA实现:SPFA速度比Bellman_Ford快多了。链表实现,表头不存数据。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>

using namespace std;

const int INF = 10000000;
const int MAXN = 1010;

struct ArcNode
{
int to;
int weight;
ArcNode* pNext;

ArcNode()
{
to = 0;
weight = 0;
pNext = NULL;
}
};

ArcNode* List[MAXN];

int n;
int dist[MAXN];
bool inq[MAXN];

bool SPFA()
{
int i;
int kcount[MAXN];
queue < int > Q;
for (i = 0; i <= n; ++i)
{
dist[i] = INF;
inq[i] = false;
kcount[i] = 0;
}
dist[0] = 0;
dist
= 0;
Q.push( n );
kcount
++;

while (!Q.empty())
{
int u = Q.front();
if(kcount[u] > n)
return false;
Q.pop();
inq[u] = false;

ArcNode* ptr = List[u]->pNext;

while (ptr != NULL)
{
int v = ptr->to;
if(dist[v] > dist[u] + ptr->weight)
{
dist[v] = dist[u] + ptr->weight;
if(!inq[v])
{
inq[v] = true;
Q.push(v);
kcount[v]++;
}
}
ptr = ptr->pNext;
}
}
return true;
}

int main()
{
int m;
int u, v, w;
int i;
int d[MAXN];
int C[MAXN];
while (~scanf("%d %d",&n, &m))
{
memset(d, 0, sizeof(d));
memset(C, 0, sizeof(C));
for (i = 0; i <= n; ++i)
{
List[i] = new ArcNode;
}
for (i = 1; i <= n; ++i)
{
cin>>C[i];
ArcNode* temp = new ArcNode;
temp->to = i;
temp->weight = C[i];
temp->pNext = List[i-1]->pNext;
List[i-1]->pNext = temp;

temp = new ArcNode;
temp->to = i-1;
temp->weight = 0;
temp->pNext = List[i]->pNext;
List[i]->pNext = temp;
d[i] = C[i] + d[i-1];
}

for (i = 0; i < m; ++i)
{
cin>>u>>v>>w;
ArcNode* temp = new ArcNode;
temp->to = u-1;
temp->weight = -w;
temp->pNext = List[v]->pNext;
List[v]->pNext = temp;

temp = new ArcNode;
temp->to = v;
temp->weight = d[v] - d[u-1];
temp->pNext = List[u-1]->pNext;
List[u-1]->pNext = temp;
}

if(SPFA())
cout<<dist
- dist[0]<<endl;
else
cout<<"Bad Estimations"<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: