Google APAC 2016 roundA gCampus
2015-08-27 20:50
561 查看
Floyd处理,在发现某条路径有更短的时候认为它inefficient,然后输出;
刚拿到的时候就认为是这样处理,但是不对,后来可以练习时候观察还是应该这样,不知道哪里有问题,如果有觉得这样的处理有漏洞的还烦请告知,谢谢,思路比较通用,代码应该还易懂,谢谢
刚拿到的时候就认为是这样处理,但是不对,后来可以练习时候观察还是应该这样,不知道哪里有问题,如果有觉得这样的处理有漏洞的还烦请告知,谢谢,思路比较通用,代码应该还易懂,谢谢
#include <cstdio> #include <vector> #include <cmath> #include<map> #include <set> using namespace std; int dis[110][110] = { 0 }; int roadNum[110][110]; vector<vector<int> > edgeRedundant;//相当于哈希索引一样 int main(){ <span style="white-space:pre"> </span>int T; <span style="white-space:pre"> </span>//freopen("G:\\input.txt", "r", stdin); <span style="white-space:pre"> </span>freopen("G:\\C-small-practice2.in", "r", stdin); <span style="white-space:pre"> </span>freopen("G:\\out.txt", "w", stdout); <span style="white-space:pre"> </span>scanf("%d", &T); <span style="white-space:pre"> </span>for (int t = 1; t <= T; t++){ <span style="white-space:pre"> </span>set<int> res; <span style="white-space:pre"> </span>printf("Case #%d:\n", t); <span style="white-space:pre"> </span>int N, M;//N:维数,M:查询数目 <span style="white-space:pre"> </span>scanf("%d%d", &N, &M); <span style="white-space:pre"> </span>edgeRedundant=vector<vector<int> >(N); <span style="white-space:pre"> </span>vector<vector<int> > oldDis(N, vector<int>(N, 0)); <span style="white-space:pre"> </span>memset(dis, 0x3F, N*sizeof(dis[0])); <span style="white-space:pre"> </span>for (int m = 0; m < M; m++) { <span style="white-space:pre"> </span>int a, b, c; <span style="white-space:pre"> </span>scanf("%d%d%d", &a, &b, &c); <span style="white-space:pre"> </span>if (dis[a][b] != 0x3F3F3F3F){//已经存在了 <span style="white-space:pre"> </span>edgeRedundant[roadNum[a][b]].push_back(m); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>else{ <span style="white-space:pre"> </span>dis[a][b] = dis[b][a] = c; <span style="white-space:pre"> </span>oldDis[a][b] = oldDis[b][a] = c; <span style="white-space:pre"> </span>roadNum[a][b] = roadNum[b][a] = m; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>//floyd <span style="white-space:pre"> </span>for (int k = 0; k < N; k++){ <span style="white-space:pre"> </span>for (int i = 0; i < N; i++){ <span style="white-space:pre"> </span>if (i == k) <span style="white-space:pre"> </span>continue; <span style="white-space:pre"> </span>for (int j = 0; j < N; j++){ <span style="white-space:pre"> </span>if (i == j || j == k) <span style="white-space:pre"> </span>continue; <span style="white-space:pre"> </span>if (dis[i][j]>dis[i][k] + dis[k][j]){ <span style="white-space:pre"> </span>if (oldDis[i][j] != 0){//inefficient <span style="white-space:pre"> </span>int goal=roadNum[i][j]; <span style="white-space:pre"> </span>res.insert(goal); <span style="white-space:pre"> </span>if(edgeRedundant[goal].size()!=0){ <span style="white-space:pre"> </span>res.insert(edgeRedundant[goal].begin(),edgeRedundant[goal].end()); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>dis[i][j] = dis[i][k] + dis[k][j]; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}//j <span style="white-space:pre"> </span>}//for i <span style="white-space:pre"> </span>}//for k <span style="white-space:pre"> </span>for (set<int>::iterator it = res.begin(); it != res.end(); it++) <span style="white-space:pre"> </span>printf("%d\n", *it); <span style="white-space:pre"> </span>memset(roadNum, 0xFF, sizeof(roadNum)); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>return 0; }
相关文章推荐
- django ajax使用
- LightOJ 1152 - Hiding Gold【二分图】
- [HDU 3966] Aragorn's Story 树链剖分
- 关于google的历史纪录和书签
- POJ 3228 -- Gold Transportation【二分 && 最大流】
- leetcode: (125) Valid Palindrome
- Chrome实验版可同时检查多种语言拼写错误
- Goods:注册页面实现
- 如何下载google play免费应用的apk文件
- 华为oj: 输入n个整数,输出其中最小的k个
- 使用Category 重写frame
- Go语言并发编程(一)
- django学习笔记
- django 日志配置
- zygote oom_score_adj 被设置成1000
- Google 的开源技术protobuf 简介与例子
- 华为oj: 字符串加解密
- 【索引】Geometric Algorithms in 2D::Exercises: Beginner
- ZOJ 3751 Diagonal
- 【索引】Geometric Algorithms in 2D::Examples