POJ1949 (DAG图的最长路径)
来源:互联网 发布:软件设计师考试解析 编辑:程序博客网 时间:2024/05/21 14:07
Chores
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 5834 Accepted: 2758
Description
Farmer John’s family pitches in with the chores during milking, doing all the chores as quickly as possible. At FJ’s house, some chores cannot be started until others have been completed, e.g., it is impossible to wash the cows until they are in the stalls.
Farmer John has a list of N (3 <= N <= 10,000) chores that must be completed. Each chore requires an integer time (1 <= length of time <= 100) to complete and there may be other chores that must be completed before this chore is started. We will call these prerequisite chores. At least one chore has no prerequisite: the very first one, number 1. Farmer John’s list of chores is nicely ordered, and chore K (K > 1) can have only chores 1,.K-1 as prerequisites. Write a program that reads a list of chores from 1 to N with associated times and all perquisite chores. Now calculate the shortest time it will take to complete all N chores. Of course, chores that do not depend on each other can be performed simultaneously.
Input
Line 1: One integer, N
Lines 2..N+1: N lines, each with several space-separated integers. Line 2 contains chore 1; line 3 contains chore 2, and so on. Each line contains the length of time to complete the chore, the number of the prerequisites, Pi, (0 <= Pi <= 100), and the Pi prerequisites (range 1..N, of course).
Output
A single line with an integer which is the least amount of time required to perform all the chores.
Sample Input
7
5 0
1 1 1
3 1 2
6 1 1
1 2 2 4
8 2 2 4
4 3 3 5 6
Sample Output
23
解法很多。这里只是想学习一下最长路径
代码:
#include<iostream>#include <stdio.h>#include <stdlib.h>#include <queue>#include <vector>#include <string.h>#include <algorithm>using namespace std;const int N=10005;vector<int>ch[N];int c[N];int dp[N];bool vis[N];queue<int>q;int x,y,n,k;int bfs(){ memset(vis,false,sizeof(vis)); memset(dp,0,sizeof(dp)); int ans=0; q.push(0); while(!q.empty()) { int x=q.front();q.pop(); vis[x]=false; int up=ch[x].size(); for(int i=0;i<up;i++) { int y=ch[x][i]; if(dp[y]>=dp[x]+c[y]) continue; dp[y]=dp[x]+c[y]; if(ch[y].size()==0) ans=max(ans,dp[y]); else if(!vis[y]) { q.push(y); vis[y]=true; } } } return ans;}int main(){ while(~scanf("%d",&n)) { for(int i=0;i<=n;i++) ch[i].clear(); for(int i=1;i<=n;i++) { scanf("%d%d",&c[i],&k); if(k==0) ch[0].push_back(i); else { while(k--) { scanf("%d",&x); ch[x].push_back(i); } } } printf("%d\n",bfs()); }}
- POJ1949 (DAG图的最长路径)
- poj1949 DAG 最长路
- POJ1949 Chores 解题报告【拓扑排序/DAG最长路】
- POJ-1949(DAG最长路径)
- HDU 4109 Instrction Arrangement(DAG上的最长路径)记忆化搜索
- uva 103 Stacking Boxes(DAG上的最长路径)
- poj 3018 gift boxes DAG上的最长路径
- 20161026的考试】KMP,二维LIS,DAG删一个点求最长路径最小值(BZOJ 3832)
- hdu 4109 Instrction Arrangement【DAG图上的最长路】
- UVA 103 Stacking Boxes (dp + DAG上的最长路径 + 记忆化搜索)
- HDU:4109 Instrction Arrangement (DAG上的最长路/关键路径)
- 图的最长路径
- 图的最长路径
- hdu 1151 DAG图(有向无环图)的最小路径覆盖
- hdu 1151 Air Raid DaG 图的最小路径覆盖
- hdu 1151DAG图的最小路径覆盖数
- hdu1151 Air Raid,DAG图的最小路径覆盖
- UVAlive3126 Taxi Cab Scheme(DAG的最小路径覆盖)
- 有一个理财管理数据库,包括客户Client,资金账户Fund,经理人Manager,投资流水Investment,理财产品Product。 回答以下问题:
- Java中Synchronized的用法
- Xen半虚拟化下,IO共享环、事件通道、授权表之间的联系和区别
- 无线安全专题01--kali破解WPA
- linux编译android无法找到"jni.h"“jni_md.h”的头文件怎么办?
- POJ1949 (DAG图的最长路径)
- 定时取消订单,防止刷新重新开始
- 【bzoj 1088】 [SCOI2005]扫雷Mine 不能再水了
- Oracle 相关记录
- 207,210. Course Schedule I, II
- Swift-工厂方法(Factory Method)
- Java JDBC链接数据库使用方式 java JDBC链接SQlServer数据库
- Unity CardboardSDK解析
- Java synchronized详解