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 ...
随机推荐
- 杭电21题 Palindrome
Problem Description A palindrome is a symmetrical string, that is, a string read identically from le ...
- display flex 和a标签不行
父元素display: flex; display: -webkit-flex; flex-flow: row wrap; -webkit-flex-flow: row wrap; 配合子元素 fl ...
- 笨方法学python--提示别人
1 上次学到使用raw_input(), 还可以如下使用: age = raw_input("age?") 2 命令名查看raw_input的说明 unit, pydoc raw_ ...
- redis持久化探究
redis支持两种持久化方式,一种是RDB方式,另一种是AOF方式.redis3.0windows版本默认关闭AOF(appendonly no),而开启RDB,当达到一定条件时,redis就会将内存 ...
- 转:java.io.IOException: Exceeeded maximum number of redirects: 5 解决版本
Jmeter运行的时候出现的重定向超过n次的问题: When trying to test a Silverlight application, I get the below error. Has ...
- window2008 64位系统无法调用Microsoft.Office.Interop组件进行文件另存的解决办法
生成execl时遇到的问题: 检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005 ...
- hdu_5776_sum(前缀和维护)
题目链接:hdu_5776_sum 题意: 给你一串数,问你是否有一个连续的子序列的和为m的倍数 题解: 维护一个前缀和%m的值,如果前缀和%m的值为0或者有两个前缀和%m的值相同,那么就有一个连续区 ...
- hdu_1011_Starship Troopers(树形DP)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 题意:有N个房间,房间的连通性为树形的,就是说你要占领子结点,必须要先占领 父结点,每个房间有第 ...
- LeetCode OJ 222. Count Complete Tree Nodes
Total Accepted: 32628 Total Submissions: 129569 Difficulty: Medium Given a complete binary tree, cou ...
- java使用iText生成pdf表格
转载地址:http://www.open-open.com/code/view/1424011530749 首先需要你自己下载itext相关的jar包并添加引用,或者在maven中添加如下引用配置: ...