标签:instrction arrangeme hdu 4109 差分约束
5 2 1 2 1 3 4 1
2HintIn the 1st ns, instruction 0, 1 and 3 are executed; In the 2nd ns, instruction 2 and 4 are executed. So the answer should be 2.
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
typedef long long LL;
const int MAXM = 100000;
const int MAXN = 1005;
struct Edge
{
int u,v,len,next;
}edge[MAXM];
int n,m,num;
int head[MAXN],dist[MAXN],inq[MAXN];
void init()
{
num=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int len)
{
edge[num].u=u;
edge[num].v=v;
edge[num].len=len;
edge[num].next=head[u];
head[u]=num++;
}
void spfa()
{
memset(dist,INF,sizeof(dist));
memset(inq,0,sizeof(inq));
inq[0]=1;
dist[0]=0;
queue<int>Q;
Q.push(0);
while (!Q.empty())
{
int u=Q.front();
Q.pop();
inq[u]=0;
for (int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].v;
if (dist[v]>dist[u]+edge[i].len)
{
dist[v]=dist[u]+edge[i].len;
if (!inq[v])
{
inq[v]=1;
Q.push(v);
}
}
}
}
printf("%d\n",-dist[n+1]);
}
int main()
{
int i,j,u,v,w;
while (~scanf("%d%d",&n,&m))
{
init();
for (i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
u++,v++;
addedge(v,u,-w);
}
for (i=1;i<=n;i++)
{
addedge(0,i,0);
addedge(i,n+1,-1);
}
spfa();
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Instrction Arrangement (hdu 4109 差分约束)
标签:instrction arrangeme hdu 4109 差分约束
原文地址:http://blog.csdn.net/u014422052/article/details/47987273