用s[i]表示小于等于i的元数个数
可得约束条件为:s[node.from]-s[node.to]<-c,隐含的为0<=s[i]-s[i-1]<=1
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=50010;
struct cnode
{
int from;
int to;
int w;
}node[MAXN];
int d[MAXN];
int n;
int mmin;
int mmax;
bool bellman_ford()
{
memset(d,0,sizeof(d));
bool flag=1;
bool f;
int i;
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
if(d[node[i].from]>d[node[i].to]+node[i].w)//from-to<c
{
d[node[i].from]=d[node[i].to]+node[i].w;
flag=1;
}
}
for(i=mmin;i<=mmax;i++)
{
if(d[i-1]+1<d[i])//0<=d[i]-d[i-1]<=1
{
d[i]=d[i-1]+1;
f=1;
}
}
for(i=mmax;i>=mmin;i--)
{
if(d[i-1]>d[i])
{
d[i-1]=d[i];
f=1;
}
}
}
return true;
}
int main()
{
int FROM,TO,W;
int i;
while(scanf("%d",&n)!=EOF)
{
mmin=800000000;
mmax=-100;
for(i=0;i<n;i++)
{
scanf("%d %d %d",&FROM,&TO,&W);
node[i].from=FROM-1;
node[i].to=TO;
node[i].w=-W;
if(FROM<mmin)
{
mmin=FROM;
}
if(TO>mmax)
{
mmax=TO;
}
}
bellman_ford();
printf("%d/n",d[mmax]-d[mmin-1]);
return 0;
}
return 0;
}
附题目:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13145 | Accepted: 4860 |
Description
Input
Output
Sample Input
5 3 7 3 8 10 3 6 8 1 1 3 1 10 11 1
Sample Output
6