POJ3249 Test for Job DAG最短路

  题目链接:http://poj.org/problem?id=3249

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

  1 //STATUS:C++_AC_2000MS_14272KB
  2 #include <functional>
  3 #include <algorithm>
  4 #include <iostream>
  5 //#include <ext/rope>
  6 #include <fstream>
  7 #include <sstream>
  8 #include <iomanip>
  9 #include <numeric>
 10 #include <cstring>
 11 #include <cassert>
 12 #include <cstdio>
 13 #include <string>
 14 #include <vector>
 15 #include <bitset>
 16 #include <queue>
 17 #include <stack>
 18 #include <cmath>
 19 #include <ctime>
 20 #include <list>
 21 #include <set>
 22 #include <map>
 23 using namespace std;
 24 //define
 25 #define pii pair<int,int>
 26 #define mem(a,b) memset(a,b,sizeof(a))
 27 #define lson l,mid,rt<<1
 28 #define rson mid+1,r,rt<<1|1
 29 #define PI acos(-1.0)
 30 //typedef
 31 typedef __int64 LL;
 32 typedef unsigned __int64 ULL;
 33 //const
 34 const int N=100010;
 35 const int INF=0x3f3f3f3f;
 36 const int MOD=100000,STA=8000010;
 37 const LL LNF=1LL<<60;
 38 const double EPS=1e-8;
 39 const double OO=1e15;
 40 const int dx[4]={-1,0,1,0};
 41 const int dy[4]={0,1,0,-1};
 42 //Daily Use ...
 43 inline int sign(double x){return (x>EPS)-(x<-EPS);}
 44 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
 45 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
 46 template<class T> inline T Min(T a,T b){return a<b?a:b;}
 47 template<class T> inline T Max(T a,T b){return a>b?a:b;}
 48 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
 49 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
 50 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
 51 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
 52 //End
 53 
 54 struct Edge{
 55     int u,v;
 56 }e[N*10];
 57 int first[N],next[N*10],val[N],cnti[N],cnto[N];
 58 LL d[N];
 59 int n,m,mt;
 60 
 61 void adde(int a,int b)
 62 {
 63     e[mt].u=a;e[mt].v=b;
 64     next[mt]=first[a];first[a]=mt++;
 65 }
 66 
 67 LL dfs(int u)
 68 {
 69     if(d[u]!=-LNF)return d[u];
 70     if(!cnto[u])return d[u]=val[u];
 71     int i;
 72     for(i=first[u];i!=-1;i=next[i]){
 73         d[u]=Max(d[u],(LL)val[u]+dfs(e[i].v));
 74     }
 75     return d[u];
 76 }
 77 
 78 int main()
 79 {
 80   //  freopen("in.txt","r",stdin);
 81     int i,j,a,b;
 82     LL ans;
 83     while(~scanf("%d%d",&n,&m))
 84     {
 85         for(i=1;i<=n;i++)
 86             scanf("%d",&val[i]);
 87         mt=0;
 88         mem(first,-1);
 89         mem(cnti,0);mem(cnto,0);
 90         for(i=1;i<=m;i++){
 91             scanf("%d%d",&a,&b);
 92             cnti[b]++,cnto[a]++;
 93             adde(a,b);
 94         }
 95 
 96         ans=-LNF;
 97         for(i=1;i<=n;i++)d[i]=-LNF;
 98         for(i=1;i<=n;i++){
 99             if(!cnti[i]){
100                 ans=Max(ans,dfs(i));
101             }
102         }
103 
104         printf("%I64d\n",ans);
105     }
106     return 0;
107 }
原文地址:https://www.cnblogs.com/zhsl/p/3090465.html