08-图9 关键活动 (30分)
假定一个工程项目由一组子任务构成,子任务之间有的可以并行执行,有的必须在完成了其它一些子任务后才能执行。“任务调度”包括一组子任务、以及每个子任务可以执行所依赖的子任务集。
比如完成一个专业的所有课程学习和毕业设计可以看成一个本科生要完成的一项工程,各门课程可以看成是子任务。有些课程可以同时开设,比如英语和C程序设计,它们没有必须先修哪门的约束;有些课程则不可以同时开设,因为它们有先后的依赖关系,比如C程序设计和数据结构两门课,必须先学习前者。
但是需要注意的是,对一组子任务,并不是任意的任务调度都是一个可行的方案。比如方案中存在“子任务A依赖于子任务B,子任务B依赖于子任务C,子任务C又依赖于子任务A”,那么这三个任务哪个都不能先执行,这就是一个不可行的方案。
任务调度问题中,如果还给出了完成每个子任务需要的时间,则我们可以算出完成整个工程需要的最短时间。在这些子任务中,有些任务即使推迟几天完成,也不会影响全局的工期;但是有些任务必须准时完成,否则整个项目的工期就要因此延误,这种任务就叫“关键活动”。
请编写程序判定一个给定的工程项目的任务调度是否可行;如果该调度方案可行,则计算完成整个工程项目需要的最短时间,并输出所有的关键活动。
输入格式:
输入第1行给出两个正整数N(≤100)和M,其中N是任务交接点(即衔接相互依赖的两个子任务的节点,例如:若任务2要在任务1完成后才开始,则两任务之间必有一个交接点)的数量。交接点按1N编号,M是子任务的数量,依次编号为1M。随后M行,每行给出了3个正整数,分别是该任务开始和完成涉及的交接点编号以及该任务所需的时间,整数间用空格分隔。
输出格式:
如果任务调度不可行,则输出0;否则第1行输出完成整个工程项目需要的时间,第2行开始输出所有关键活动,每个关键活动占一行,按格式“V->W”输出,其中V和W为该任务开始和完成涉及的交接点编号。关键活动输出的顺序规则是:任务开始的交接点编号小者优先,起点编号相同时,与输入时任务的顺序相反。
输入样例:
7 8
1 2 4
1 3 3
2 4 5
3 4 3
4 5 1
4 6 6
5 7 5
6 7 2
输出样例:
17
1->2
2->4
4->6
6->7
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
#define INFINITY 65535
#define MaxVertexNum 200
int InDegree[MaxVertexNum],TNode[MaxVertexNum][MaxVertexNum]={0};
struct ENode {
int V1,V2;
int Weight;
};
typedef struct ENode* Edge;
struct AdjVNode {
int AdjX;
int weight;
struct AdjVNode* Next;
};
typedef struct AdjVNode* PtrAdjVNode;
struct VNode {
PtrAdjVNode FirstNode;
int weight;
int min;
};
struct LNode {
int Nv;
int Ne;
VNode V[MaxVertexNum];
};
typedef struct LNode *LGraph;
void InsertEdge(LGraph Graph,Edge E) {
PtrAdjVNode NewNode=(PtrAdjVNode)malloc(sizeof(struct AdjVNode));
NewNode->AdjX=E->V2;
NewNode->weight=E->Weight;
NewNode->Next=Graph->V[E->V1].FirstNode;
Graph->V[E->V1].FirstNode=NewNode;
}
LGraph Create() {
LGraph Graph=(LGraph)malloc(sizeof(struct LNode));
cin>>Graph->Nv>>Graph->Ne;
Edge E=(Edge)malloc(sizeof(struct ENode));
for(int i=1; i<=Graph->Nv; i++) {
Graph->V[i].FirstNode=NULL;
Graph->V[i].weight=0;
Graph->V[i].min=INFINITY;
InDegree[i]=0;
}
for(int i=1; i<=Graph->Nv; i++)
for(int j=1; j<=Graph->Nv; j++){
TNode[i][j]=INFINITY;
}
for(int i=1; i<=Graph->Ne; i++) {
cin>>E->V1>>E->V2>>E->Weight;
InsertEdge(Graph,E);
TNode[E->V2][E->V1]=E->Weight;
}
return Graph;
}
void Prim(LGraph Graph) {
int V,cnt=0,max=0,flag=1;
queue<int>Q;
for(int i=1; i<=Graph->Nv; i++) {
for(PtrAdjVNode W=Graph->V[i].FirstNode; W; W=W->Next) {
InDegree[W->AdjX]++;
}
}
for(int i=1; i<=Graph->Nv; i++) {
if(InDegree[i]==0) {
Q.push(i);
}
}
stack<int>S;
while(!Q.empty()) {
V=Q.front();
Q.pop();
cnt++;
S.push(V);
for(PtrAdjVNode W=Graph->V[V].FirstNode; W; W=W->Next) {
InDegree[W->AdjX]--;
if(Graph->V[W->AdjX].weight<Graph->V[V].weight+W->weight) {
Graph->V[W->AdjX].weight=Graph->V[V].weight+W->weight;
}
if(InDegree[W->AdjX]==0) {
Q.push(W->AdjX);
}
}
}
if(cnt!=Graph->Nv) {
printf("0");
} else {
for(int i=1; i<=Graph->Nv; i++) {
if(max<Graph->V[i].weight) {
max=Graph->V[i].weight;
}
}
printf("%d\n",max);
for(int i=1;i<=Graph->Nv;i++){
Graph->V[i].min=max;
}
int x;
while(!S.empty()){
x=S.top();S.pop();
for(int i=1;i<=Graph->Nv;i++){
if(TNode[x][i]!=INFINITY){
if(Graph->V[i].min>Graph->V[x].min-TNode[x][i]){
Graph->V[i].min=Graph->V[x].min-TNode[x][i];
}
}
}
}
for(int i=1; i<=Graph->Nv; i++) {
for(PtrAdjVNode W=Graph->V[i].FirstNode; W; W=W->Next) {
if(Graph->V[i].weight==Graph->V[W->AdjX].min-W->weight) {
if(flag){
printf("%d->%d",i,W->AdjX);
flag=0;
}else{
printf("\n%d->%d",i,W->AdjX);
}
}
}
}
}
}
int main() {
LGraph Graph=Create();
Prim(Graph);
}
#include<bits/stdc++.h>
using namespace std;
#define INF 65536
struct Node {
int data, weight;
Node(int a, int b) {
data = a;
weight = b;
}
};
int N, M, earlist[105] = {0}, lastest[105], Indegree[105] = {0}, Outdegree[105] = {0}, counts = 0, MaxTime = 0;
vector<Node> Vs[105], Ve[105];
int main () {
cin >> N >> M;
fill(lastest + 1, lastest + N + 1, INF);
int v, u, w;
for (int i = 0; i < M; i++) {
cin >> v >> u >> w;
Node node = Node(u,w);
Indegree[u]++;
Outdegree[v]++;
Vs[v].push_back(node);
Node temp = Node(v, w);
Ve[u].push_back(temp);
}
queue<int> Q;
for (int i = 1; i <= N; i++) {
if (Indegree[i] == 0) {
Q.push(i);
earlist[i] = 0;
}
}
while (!Q.empty()) {
int temp = Q.front();
Q.pop();
counts++;
if (Vs[temp].size() == 0) {
MaxTime = max(MaxTime, earlist[temp]);
}
for (int i = 0; i < Vs[temp].size(); i++) {
int Vertex = Vs[temp][i].data;
Indegree[Vertex]--;
if (Indegree[Vertex] == 0) Q.push(Vertex);
if (earlist[Vertex] < Vs[temp][i].weight + earlist[temp]) {
earlist[Vertex] = Vs[temp][i].weight + earlist[temp];
}
}
}
if (counts != N) {
cout<< "0";
return 0;
} else cout << MaxTime << endl;
for (int i = 1; i <= N; i++) {
if (Outdegree[i] == 0) {
Q.push(i);
lastest[i] = MaxTime;
}
}
while (!Q.empty()) {
int temp = Q.front();
Q.pop();
for (int i = 0; i < Ve[temp].size(); i++) {
int Vertex = Ve[temp][i].data;
Outdegree[Vertex]--;
if (Outdegree[Vertex] == 0) Q.push(Vertex);
if (lastest[Vertex] > lastest[temp] - Ve[temp][i].weight) {
lastest[Vertex] = lastest[temp] - Ve[temp][i].weight;
}
}
}
for (int i = 1; i <= N; i++) {
for (int j = Vs[i].size() - 1; j >= 0; j--) {
int Vertex = Vs[i][j].data;
if (earlist[i] + Vs[i][j].weight == lastest[Vertex]) {
cout << i << "->" << Vertex << endl;
}
}
}
return 0;
}