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 ...
随机推荐
- javacript参数传递表单验证
<!doctype html> <html> <head> <meta charset="utf-8"> <style typ ...
- InvalidateRect只是增加重绘区域,在下次WM_PAINT的时候才生效
emWIN里面的无效重绘和windows很类似. WM_InvalidateArea()和WM_InvalidateRect()只重绘指定的区域,其他区域不会重绘,这样避免了闪烁,重绘发生在下次WM_ ...
- Java heap space
//首先检查程序有没有限入死循环 这个问题主要还是由这个问题 java.lang.OutOfMemoryError: Java heap space 引起的.第一次出现这样的的问题以后,引发了其他的问 ...
- IDEA类文件不编译问题
用IDEA的人遇到过类文件上有个小叉吗? 1.在 .gitignore 里面把这个文件去掉 2.setting->builder->compiler->子目录 去掉不编译的文件
- C语言隐式强制类型转换
今天又被精度问题困扰,把最基本的东西忘了. int n = 5; int cnt = 5.5; double sum = (n-cnt); 运算完后sum是 -0.5.不知道什么时候n转换成doub ...
- nefu 115 斐波那契的整除
Description 已知斐波那契数列有如下递归定义,f(1)=1,f(2)=1, 且n>=3,f(n)=f(n-1)+f(n-2),它的前几项可以表示为1, 1,2 ,3 ,5 ,8,13, ...
- 删除 mysql 日志文件后 ,启动出错
把 mysql-bin.index 里面的索引全部删除
- 第4章 流程控制----编写Java程序,应用for循环打印菱形
package four; public class fouroneone { public static void main(String args[]){ ;i<=;i+=){ ;kong& ...
- Idea 设置根目录
1.在根目录下新建一个目录yx360-war-ctm-tea,在该目录下新建一个build.gradle文件,输入: apply plugin: 'war' 来引入war插件,war插件会在项目的目录 ...
- ubuntu 连接VPN 命令
1 注册MXVPN 2 启动VPN pptpsetup --create mxvpn1 --server xx.xx.xx.xx --username *** --password *** --e ...