拓扑排序 hdu 2647
来源:互联网 发布:贵阳大数据 编辑:程序博客网 时间:2024/05/17 21:02
反向建边,因为最后的是888,所以反向建进行拓扑排序,再用一个数组存储拓扑当前位置的金额;
#include<stdio.h>#include<algorithm>#include<queue>#include<string.h>#include<string>#include<map>using namespace std;vector<int>g[10005];int indegree[10005];int price[10005];int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { memset(indegree,0,sizeof(indegree)); memset(price,0,sizeof(price)); for(int i=1;i<=n;i++)g[i].clear(); for(int i=0;i<m;i++) { int a,b; scanf("%d%d",&a,&b); g[b].push_back(a); indegree[a]++; } queue<int>q; int k=0,cont=n; for(int i=1;i<=n;i++) { if(indegree[i]==0) { q.push(i); price[i]=888; } } while(!q.empty()) { cont--; int f=q.front(); q.pop(); for(int i=0;i<g[f].size();i++) { indegree[g[f][i]]--; if(indegree[g[f][i]]==0) { q.push(g[f][i]); price[g[f][i]]=price[f]+1; } } } int sum=0; for(int i=1;i<=n;i++) sum+=price[i]; if(cont>0) printf("-1\n"); else printf("%d\n",sum); for(int i=0;i<=n;i++) g[i].clear(); }}
阅读全文
0 0
- HDU-2647拓扑排序
- hdu 2647 拓扑排序
- hdu 2647 拓扑排序
- 拓扑排序 HDU 2647
- hdu 2647 拓扑排序
- hdu 2647 拓扑排序
- hdu 2647(拓扑排序)
- hdu 2647 拓扑排序
- hdu 2647拓扑排序
- hdu 2647 拓扑排序
- 拓扑排序 hdu 2647
- hdu 2647 Reward 拓扑排序
- hdu 2647 Reward 拓扑排序
- hdu 2647 Reward 拓扑排序
- hdu 2647 bfs+拓扑排序
- hdu 2647 Reward 拓扑排序
- hdu 2647拓扑排序 容器
- hdu 2647(拓扑排序)
- 极光推送,如何接收推送下来的信息。
- 创建Maven web项目时 出现 web.xml is missing and <failOnMissingWebXml> is set to true错误 pox.xml编译错误
- 简单选择排序(js)
- HDU -1598 find the most comfortable road
- spring常用的一些注解以及注解注入总结(二)
- 拓扑排序 hdu 2647
- Codevs 1163:访问艺术馆——题解
- 【bzoj2705】[SDOI2012]Longge的问题
- 欢迎使用CSDN-markdown编辑器
- 什么是DTD?怎样使用?
- python之路——文件操作
- zhihu-spider之RabbitMQ——zhihu-spider开源项目使用技术详解(其五)
- _CRT_SECURE_NO_DEPRECATE
- Android 混淆那些事儿