cug1697 spfa+邻接表
来源:互联网 发布:人工智能概念股一览 编辑:程序博客网 时间:2024/05/19 04:04
题目大意:赤壁决战,曹操在有利形势下,轻敌自负,指挥失误,终致战败。孙权、刘备在强敌进逼关头,结盟抗战,扬水战之长,巧用火攻,终以弱胜强。赤壁之战结束之后魏蜀吴三国决定握手求和,但是因为之前四处交战,把道路全部毁坏了,现在需要重新修路使得三国的国都能够连通.诸葛亮是这个项目的负责人,但是他太聪明了,对于这种题目都懒得思考了,于是就交给你了。
思路:因为要求三个和城市之间的最短路,最短路一定是经过某一点到三点距离和最小,不是一个城市到其他两个城市的最短路。所以3遍spfa把三个城市的最短路跑出来,然后枚举中间点即可。
无法连通输出-1;
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <fstream>#include <algorithm>#include <cmath>#include <queue>#include <stack>#include <vector>#include <map>#include <set>#include <iomanip>using namespace std;//#pragma comment(linker, "/STACK:102400000,102400000")#define maxn 200005#define MOD 1000000007#define mem(a , b) memset(a , b , sizeof(a))#define LL long long#define ULL unsigned long longtypedef pair<int , int> pii;const long long INF= 0x3fffffff;int n , m;struct Edge{ int u , v , w , next;}edge[maxn];int head[maxn] , id;int cost1[maxn] , cost2[maxn] , cost3[maxn];void addedge(int u , int v , int w){ edge[id].u = u; edge[id].v = v; edge[id].w = w; edge[id].next = head[u]; head[u] = id ++;}void spfa(int root , int cost[]){ for(int i = 0 ; i <= n ; i ++) cost[i] = 90000000; cost[root] = 0; queue<int>q; q.push(root); while(!q.empty()) { int u = q.front(); q.pop(); for(int i = head[u] ; i != -1 ; i = edge[i].next) { if(cost[edge[i].v] > cost[u] + edge[i].w) { q.push(edge[i].v); cost[edge[i].v] = cost[u] + edge[i].w; } } }}int main(){ int t; scanf("%d" , &t); while(t--) { mem(head , -1); id = 0; scanf("%d %d" , &n , &m); int u ,v , w; for(int i = 0 ; i < m ; i ++) { scanf("%d %d %d" , &u , &v , &w); addedge(u , v , w); addedge(v , u , w); } int a , b , c; scanf("%d %d %d" , &a , &b , &c); spfa(a , cost1); spfa(b , cost2); spfa(c , cost3); long long ans = INF; for(int i = 1 ; i <= n ; i ++) { ans = min(ans , (LL)cost1[i] + cost2[i] + cost3[i]); } if(cost1[b] == cost1[c] && cost1[b] == 90000000 || cost2[a] == cost1[c] && cost2[a] == 90000000 || cost3[b] == cost3[a] && cost3[b] == 90000000) { printf("-1\n"); } else cout << ans << endl; } return 0;}
0 0
- cug1697 spfa+邻接表
- spfa+邻接表
- SPFA 邻接表
- SPFA+邻接表
- 邻接表的spfa
- [模板]spfa+邻接表
- SPFA 邻接表模式
- SPFA + 静态邻接表 模板
- POJ 1511 邻接表+spfa
- HDU-2544(SPFA()邻接表)
- 用邻接表实现spfa
- poj 1511(SPFA+邻接表)
- spfa+队列+邻接表POJ3159
- 邻接表(spfa模版)
- 邻接表存储图的SPFA算法~
- spfa+手写邻接表模板模板
- poj 1511 Invitation Cards (SPFA、邻接表)
- poj1511 spfa(反向保存)+邻接表
- 【给数学不好的人的KMP】字符匹配教程(二)简化运算是为了什么如何移位
- Linux常用命令(五)用户及用户组管理
- SQL Server 批量插入数据的两种方法
- 架构和框架的区别
- vs2010编译Qt工程,中文显示乱码
- cug1697 spfa+邻接表
- jQuery实现Ajax应用
- mysq中information_schema
- iOS基础教程:iOS-UI-CoreData学习笔记
- JSON数据乱码问题
- leetcode_168_Excel Sheet Column Title
- XMG Quartz2D 图片截取操作
- 概述,贝叶斯策略,最大似然估计
- 六之续、由KMP算法谈到BM算法