您的位置:首页 > 其它

POJ-3249 Test for Job DAG最短路

2013-05-21 11:55 281 查看
  题目链接:http://poj.org/problem?id=3249

  DAG图上的最短路,记忆化搜索。

//STATUS:C++_AC_2000MS_14272KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=100010;
const int INF=0x3f3f3f3f;
const int MOD=100000,STA=8000010;
const LL LNF=1LL<<60;
const double EPS=1e-8;
const double OO=1e15;
const int dx[4]={-1,0,1,0};
const int dy[4]={0,1,0,-1};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End

struct Edge{
int u,v;
}e[N*10];
int first
,next[N*10],val
,cnti
,cnto
;
LL d
;
int n,m,mt;

void adde(int a,int b)
{
e[mt].u=a;e[mt].v=b;
next[mt]=first[a];first[a]=mt++;
}

LL dfs(int u)
{
if(d[u]!=-LNF)return d[u];
if(!cnto[u])return d[u]=val[u];
int i;
for(i=first[u];i!=-1;i=next[i]){
d[u]=Max(d[u],(LL)val[u]+dfs(e[i].v));
}
return d[u];
}

int main()
{
//  freopen("in.txt","r",stdin);
int i,j,a,b;
LL ans;
while(~scanf("%d%d",&n,&m))
{
for(i=1;i<=n;i++)
scanf("%d",&val[i]);
mt=0;
mem(first,-1);
mem(cnti,0);mem(cnto,0);
for(i=1;i<=m;i++){
scanf("%d%d",&a,&b);
cnti[b]++,cnto[a]++;
adde(a,b);
}

ans=-LNF;
for(i=1;i<=n;i++)d[i]=-LNF;
for(i=1;i<=n;i++){
if(!cnti[i]){
ans=Max(ans,dfs(i));
}
}

printf("%I64d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: