Google APAC 2016 roundA gCampus

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;


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值