UVALive 6885 Flowery Trails
两次SPFA
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; const int INF=0x7FFFFFFF;
const int maxn=+;
const int Maxn=+;
int N,M;
int U[Maxn],V[Maxn],C[Maxn];
struct edge
{
int from,to,cost;
}e[Maxn];
vector<int>G[maxn];
int dis1[maxn],flag1[maxn];
int dis2[maxn],flag2[maxn]; void init()
{
for(int i=;i<maxn;i++) G[i].clear();
} void spfa()
{
queue<int>Q;
for(int i=;i<maxn;i++) dis1[i]=INF;
dis1[]=;
flag1[]=;
Q.push();
while(!Q.empty())
{
int h=Q.front();Q.pop(); flag1[h]=;
for(int i=;i<G[h].size();i++)
{
int id=G[h][i];
if(e[id].from==h)
{
if(dis1[h]+e[id].cost<dis1[e[id].to])
{
dis1[e[id].to]=dis1[h]+e[id].cost;
if(flag1[e[id].to]==)
{
flag1[e[id].to]=;
Q.push(e[id].to);
}
}
}
else if(e[id].to==h)
{
if(dis1[h]+e[id].cost<dis1[e[id].from])
{
dis1[e[id].from]=dis1[h]+e[id].cost;
if(flag1[e[id].from]==)
{
flag1[e[id].from]=;
Q.push(e[id].from);
}
}
}
}
}
} void SPFA()
{
queue<int>Q;
for(int i=;i<maxn;i++) dis2[i]=INF;
dis2[N-]=;
flag2[N-]=;
Q.push(N-);
while(!Q.empty())
{
int h=Q.front();Q.pop(); flag2[h]=;
for(int i=;i<G[h].size();i++)
{
int id=G[h][i];
if(e[id].from==h)
{
if(dis2[h]+e[id].cost<dis2[e[id].to])
{
dis2[e[id].to]=dis2[h]+e[id].cost;
if(flag2[e[id].to]==)
{
flag2[e[id].to]=;
Q.push(e[id].to);
}
}
}
else if(e[id].to==h)
{
if(dis2[h]+e[id].cost<dis2[e[id].from])
{
dis2[e[id].from]=dis2[h]+e[id].cost;
if(flag2[e[id].from]==)
{
flag2[e[id].from]=;
Q.push(e[id].from);
}
}
}
}
}
}
int main()
{
while(~scanf("%d%d",&N,&M)){
init();
for(int i=;i<=M;i++)
{
scanf("%d%d%d",&U[i],&V[i],&C[i]);
e[i].from=U[i];
e[i].to=V[i];
e[i].cost=C[i];
G[U[i]].push_back(i);
G[V[i]].push_back(i);
}
spfa();
SPFA(); int ans=;
int Len=dis1[N-];
for(int i=;i<=M;i++)
{
if(dis1[U[i]]+dis2[V[i]]+C[i]==Len||dis2[U[i]]+dis1[V[i]]+C[i]==Len)
ans=ans+C[i]+C[i];
}
printf("%d\n",ans);
}
return ;
}
UVALive 6885 Flowery Trails的更多相关文章
- UVALive 6885 Flowery Trails 最短路
Flowery Trails 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid= ...
- UVALive 6885 Flowery Trails 最短路枚举
题目连接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=129723 题意: 给你一个n点m图的边 1到n有多条最短路 ...
- HNU 13375 Flowery Trails (spfa最短路)
求最短路径覆盖的全部边权值和. 思路:分别从起点和终点两次求最短路,再比较两个点到起点的距离和他们之间的权值相加和是否等于最短路径. 这题很好 #include <cstring> #in ...
- kuangbin带你飞 最短路 题解
求一个图最短路边的办法.好像下面的那个有问题.单向边和双向边一定是有区别的.这个比较容易.参照该文的最短路网络流题目和连通图题目一题求最短路关节边 另外上述2个题目的代码好像有问题. 在UVALIVE ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- Codeforces 209 C. Trails and Glades
Vasya went for a walk in the park. The park has n glades, numbered from 1 to n. There are m trails b ...
- CodeForces 209C Trails and Glades
C. Trails and Glades time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
随机推荐
- 新版本的jquery checkbox 全选反选代码只能执行一遍,第二次就失败attr与prop区别
$("#all_check").click(function() { $("input[name='checkShop[]']").attr("che ...
- padding当高度用时出现的问题
<div class="wrap"> <div class="sudoku"> <div class="sdk-wrap ...
- 第19讲 不带参数功能FC的编程与应用
- POJ1113 Wall 凸包
题目大意:建立围墙将城堡围起来,要求围墙至少距离城堡L,拐角处用圆弧取代,求围墙的长度. 题目思路:围墙长度=凸包周长+(2*PI*L),另外不知道为什么C++poj会RE,G++就没问题. #inc ...
- Android 内存监测工具 DDMS --> Heap(转)
DDMS 的全称是Dalvik Debug Monitor Service,它为我们提供例如:为测试设备截屏,针对特定的进程查看正在运行的线程以及堆信息.Logcat.广播状态信息.模拟电话呼叫.接收 ...
- linux脚本Shell之awk详解(二)
三.printf的使用 print format 生成报表 %d 十进制有符号整数 %u 十进制无符号整数 %f 浮点数 %s 字符串 %c ...
- hdu_2838_Cow Sorting(树状数组求逆序对)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 题意:给你一串数,让你排序,只能交换相邻的数,每次交换花费交换的两个树的和,问最小交换的价值 题 ...
- kubernetes 条件需求
1. 你必须拥有一台安装有Docker的机器. 2. 你的内核必须支持 memory and swap accounting .确认你的linux内核开启了如下配置: CONFIG_RESOURCE_ ...
- SQL SERVER 2000 遍历父子关系数据的表(二叉树)获得所有子节点 所有父节点及节点层数函数
---SQL SERVER 2000 遍历父子关系數據表(二叉树)获得所有子节点 所有父节点及节点层数函数---Geovin Du 涂聚文--建立測試環境Create Table GeovinDu([ ...
- 解决Sublime Text 3中文显示乱码(tab中文方块)问题,sublime tab乱码
一.文本出现中文乱码问题 1.打开Sublime Text 3,按Ctrl+-打开控制行,复制粘贴以下python代码,然后回车运行. 2. 复制并粘贴如下代码: import urllib.requ ...