题目链接:https://vjudge.net/problem/HDU-4109

题目大意

  有 N 个指令,标号从 0 ~ N - 1,和 M 个指令间的先后关系,每个关系都有一个权值 w,表示后一个指令在前一个指令开始时间之后 w 纳秒才开始执行。现在要并发执行这些指令,问最少要多长时间才能执行完?

分析

  关键路径模板题,有多个源点和汇点,答案为所有汇点的最早开始时间的最大值。

代码如下

 #include <bits/stdc++.h>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c
#define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
#define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper); #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,0x7f,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T>
ostream &operator<<(ostream &out, vector<T> &v) {
Rep(i, v.size()) out << v[i] << " \n"[i == v.size()];
return out;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} template<class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
} inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
} //min <= aim <= max
template<typename T>
inline bool BETWEEN(const T aim, const T min, const T max) {
return min <= aim && aim <= max;
} typedef long long LL;
typedef unsigned long long uLL;
typedef vector< int > VI;
typedef vector< bool > VB;
typedef vector< char > VC;
typedef vector< double > VD;
typedef vector< string > VS;
typedef vector< LL > VL;
typedef vector< VI > VVI;
typedef vector< VB > VVB;
typedef vector< VS > VVS;
typedef vector< VL > VVL;
typedef vector< VVI > VVVI;
typedef vector< VVL > VVVL;
typedef pair< int, int > PII;
typedef pair< LL, LL > PLL;
typedef pair< int, string > PIS;
typedef pair< string, int > PSI;
typedef pair< string, string > PSS;
typedef pair< double, double > PDD;
typedef vector< PII > VPII;
typedef vector< PLL > VPLL;
typedef vector< VPII > VVPII;
typedef vector< VPLL > VVPLL;
typedef vector< VS > VVS;
typedef map< int, int > MII;
typedef unordered_map< int, int > uMII;
typedef map< LL, LL > MLL;
typedef map< string, int > MSI;
typedef map< int, string > MIS;
typedef set< int > SI;
typedef stack< int > SKI;
typedef queue< int > QI;
typedef priority_queue< int > PQIMax;
typedef priority_queue< int, VI, greater< int > > PQIMin;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 1e3 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; struct Edge{
int from, to, w;
int et, lt;
}; istream& operator>> (istream& in, Edge &x) {
in >> x.from >> x.to >> x.w;
return in;
} struct Vertex{
int in, out, value;
VI next, prev;
int et, lt; void clear() {
value = in = out = ;
next.clear();
prev.clear();
et = ;
lt = inf;
}
}; int N, M, ans;
Vertex V[maxN];
vector< Edge > E;
VI topo; void addEdge(Edge &x) {
V[x.from].next.PB(E.size());
V[x.to].prev.PB(E.size());
++V[x.to].in;
++V[x.from].out;
E.PB(x);
} void init() {
For(i, , N) V[i].clear();
E.clear();
topo.clear();
ans = ;
} void TopoSort() {
SKI sk;
For(i, , N) if(!V[i].in) sk.push(i); while(!sk.empty()) {
int tmp = sk.top(); sk.pop(); topo.PB(tmp);
Rep(i, V[tmp].next.size()) {
Edge &e = E[V[tmp].next[i]];
if(!--V[e.to].in) sk.push(e.to);
V[e.to].et = max(V[e.to].et, V[e.from].et + e.w); // 在拓扑排序的同时计算每个节点的最早发生时间
}
}
} int main(){
//freopen("MyOutput.txt","w",stdout);
//freopen("input.txt","r",stdin);
INIT();
while(cin >> N >> M) {
init();
Rep(i, M) {
Edge t;
cin >> t;
++t.from;
++t.to;
addEdge(t);
}
TopoSort();
For(i, , N) if(!V[i].out) ans = max(ans, V[i].et);
cout << ans + << endl;
}
return ;
}

最新文章

  1. AngularJS特性
  2. SQLBulkCopy使用
  3. UNITY3D ShadeSH9
  4. saiku中多cube排序问题
  5. MYSQL ERROR 1045 (28000): Access denied for user &#39;neeky&#39;@&#39;Nee&#39; (using password: YES)
  6. Java集合关于ArrayList
  7. 十天学会php第五天
  8. linux升级openssh7.4sp1
  9. Mybatis 批量添加,批量更新
  10. &lt;网络编程&gt;IO复用
  11. windows常用服务命令
  12. luogu P1445 [Violet]嘤F♂A
  13. 内核同步机制-RCU同步机制
  14. 机器学习进阶-图像特征sift-SIFT特征点 1.cv2.xfeatures2d.SIFT_create(实例化sift) 2. sift.detect(找出关键点) 3.cv2.drawKeypoints(画出关键点) 4.sift.compute(根据关键点计算sift向量)
  15. Sign in with the app-specific password you generated. If you forgot the app-specific password or need to create a new one, go to appleid.apple.com
  16. 集合框架基础知识-----java基础知识
  17. Sum All Numbers in a Range(两数之间数字总和)
  18. python数据类型之集合
  19. HBase(四)HBase集群Shell操作
  20. Visual Studio2015无法启动IIS Express Web 服务器的解决方案

热门文章

  1. 初步认识AutoMapper转载 https://www.cnblogs.com/fred-bao/p/5700776.html
  2. jmeter压测、操作数据库、分布式、 linux下运行的简单介绍
  3. Django框架(二十一)—— Django rest_framework-权限组件
  4. adb 常用命令大全
  5. nutch1.9 + solr4.72
  6. koa2实现登录注册功能(ejs+mongodb版)
  7. k8s容器-运维管理篇
  8. rest framework之路由组件
  9. 08-02-loggin-模块
  10. Unity Download