/*
第一个差分约束系统
(1)如果采用最长路径求法,则求出来的是所有可行解组中最小的
(2)如果采用最短路径求法,则求出来的是所有可行解组中最大的
由于本题求最小值,因此采用解法2
*/
#include <iostream>
#include <queue>
#define MAX_E 50005
#define MIN_VAL -1000000
#define maxv(a, b) ((a) >= (b) ? (a) : (b))
using namespace std;
//紧接表
struct edge
{
int from, to, w;
edge *next;
}*head[MAX_E + 1];
void addEdge(int from, int to, int w)
{
edge * te = new edge();
te->from = from;
te->to = to;
te->w = w;
te->next = head[from];
head[from] = te;
}
queue<int> spfaq;
int minDist[MAX_E + 1];
bool v[MAX_E + 1];
int eNum = 0, iNum;
int maxI = INT_MIN;
void spfa()
{
int from ,to, w;
//memset(v, 0, sizeof(v));
spfaq.push(0);
v[0] = true;
while(!spfaq.empty())
{
from = spfaq.front();
spfaq.pop();
v[from] = false;
edge * ce = head[from];
while(ce != NULL)
{
to = ce->to;
w = ce->w;
if(minDist[from] + w > minDist[to])
{
minDist[to] = minDist[from] + w;
if(!v[to])
{
v[to] = true;
spfaq.push(to);
}
}
ce = ce->next;
}
}
cout<<minDist[maxI + 1]<<endl;
}
int main()
{
int i, from, to, c;
cin>>iNum;
//for(i = 0; i < MAX_E; i++)
// head[i] = NULL;
for(i = 0; i < iNum; i++)
{
//cin>>from>>to>>c;
scanf("%d%d%d", &from, &to, &c);
if(from > to)
{
int temp = from;
from = to;
to = temp;
}
maxI = maxv(maxI, to);
//edge te = {from, to + 1, c};
addEdge(from, to + 1, c);
}
for(i = 0; i <= maxI; i++)
{
//edge te = {i, i + 1, 0};
addEdge(i, i + 1, 0);
//edge tee = {i + 1, i, -1};
addEdge(i + 1, i, -1);
minDist[i] = MIN_VAL;
}
minDist[0] = 0;
minDist[maxI + 1] = MIN_VAL;
spfa();
return 0;
}
POJ 1201 Intervals
最新推荐文章于 2023-10-29 16:40:28 发布