zoj 2770 Burn the Linked Camp

来源:互联网 发布:a byte of python pdf 编辑:程序博客网 时间:2024/04/30 09:03

Bellman_Ford实现:点击打开http://blog.csdn.net/hearthougan/article/details/17631941

差分约束系统的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[n] = 0;Q.push( n );kcount[n]++;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[n] - dist[0]<<endl;elsecout<<"Bad Estimations"<<endl;}return 0;}


0 0