POJ 3256 Cow Picnic SPFA
来源:互联网 发布:xbox无法连接网络 编辑:程序博客网 时间:2024/06/10 22:28
题目地址:http://poj.org/problem?id=3256
题目求的是,所有奶牛都可以到的草坪有几块
那只要利用SPFA,枚举所有草坪,求出所有奶牛草坪能否到达该草坪就好了
因为要求的是其他草坪能否到达该草坪,所以把边都反向就好了
#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;const int INF=(1<<30);const int maxn=10000+5;struct Edge{int to; int weight;Edge(int t,int w):to(t),weight(w){}};vector<vector<Edge> > G(maxn);bool inq[maxn]; //判断是否在队列里 int dist[maxn];int s[maxn];void Spfa(int s,int n){memset(inq,false,sizeof(inq));for(int i=0;i<n+1;i++) dist[i]=INF;queue<int> Q;dist[s]=0;inq[s]=true; Q.push(s);while(!Q.empty()){int u=Q.front(); Q.pop();inq[u]=false;for(int i=0;i<G[u].size();i++){int v=G[u][i].to, w=G[u][i].weight;if(dist[v]>dist[u]+w){dist[v]=dist[u]+w;if(!inq[v]) Q.push(v),inq[v]=true;}}}}int main(){int K,N,M,u,v;cin>>K>>N>>M; for(int i=0;i<K;i++)cin>>s[i];for(int i=0;i<M;i++){cin>>u>>v;G[v].push_back(Edge(u,1));}int cnt=0,ok;for(int i=1;i<=N;i++){Spfa(i,N);ok=1;for(int j=0;j<K;j++)if(dist[s[j]]==INF) {ok=0;break;}if(ok) cnt++;}cout<<cnt<<endl;return 0;}
0 0
- POJ 3256 Cow Picnic SPFA
- Poj 3256 Cow Picnic
- POJ 3256 Cow Picnic
- POJ-3256-Cow Picnic
- POJ 3256 Cow Picnic
- POJ 3256 Cow Picnic
- POJ 3256 Cow Picnic 搜索
- poj 3256 Cow Picnic 优化深搜
- POJ 3256 Cow Picnic(DFS)
- POJ 3265 Cow Picnic bfs
- Cow Picnic
- Cow Picnic
- pku 3256 Cow Picnic(简单DFS)
- Cow Picnic (P3256)
- Cow Picnic(DFS)
- [Usaco06Dec] Cow Picnic
- SPFA POJ 3268 Silver Cow Party
- poj 3268 Silver Cow Party , spfa , dijkstra
- HDU 5847 构造
- xftp 登录远程服务器
- Effective Modern C++ 条款9 用别名声明(alias declaration)代替typedef
- 推理集 —— 物质与成分及性质的研究
- SDUT2107图的深度遍历
- POJ 3256 Cow Picnic SPFA
- eclipse在线安装FatJar插件失败解决方案
- 使用 Python 创建你自己的 Shell (上)
- FP-Growth 算法MapReduce 关联规则算法
- HDU 1231 最大连续子序列
- AVL树
- 游戏伪随机
- MySQL设置
- Java并发编程:并发容器之ConcurrentHashMap