cdoj 秋实大哥带我飞 最短路走法 含0权边
//做完这题以后终于理解白书上的边为什么要那样定义了 可以很方便的在o(1) 时间内找到反向边
解法:先跑一边最短路,然后检查最短路上有没有0权边(dfs就好,但是每条边只能走一次,这里就需要用异或找反向边),最后记忆化搜索一遍(每条边也是只能走一次)
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<string> using namespace std; const int INF=;
const int MOD=; struct Edge{
int from,to,cost;
}; vector <Edge> edges;
vector <int> G[];
bool vis[];
int dis[];
bool inq[];
int f[];
int n,m; void AddEdge(int x,int y,int z){
edges.push_back((Edge){x,y,z});
edges.push_back((Edge){y,x,z});
int sz=edges.size();
G[x].push_back(sz-);
G[y].push_back(sz-);
} void SPFA(){
dis[]=;
memset(inq,,sizeof(inq));
queue<int>q;
q.push();
inq[]=;
while (!q.empty()){
int now=q.front();
int sz=G[now].size();
q.pop();
for (int i=;i<sz;i++){
Edge& e=edges[G[now][i]];
if (dis[e.to]>dis[now]+e.cost){
dis[e.to]=dis[now]+e.cost;
if (!inq[e.to]){
q.push(e.to);
inq[e.to]=;
}
}
}
inq[now]=;
}
} bool check(int now){
if (now==) return ;
bool flag=;
int sz=G[now].size();
for (int i=;i<sz;i++){
Edge& e=edges[G[now][i]];
if (!vis[G[now][i]] && dis[now]==dis[e.to]+e.cost){
vis[G[now][i]]=;
vis[G[now][i]^]=;
if (e.cost==) flag=false;
if (!check(e.to)) flag=false;
}
}
return flag;
} int work(int now){
if (f[now]!=-) return f[now];
f[now]=;
int sz=G[now].size();
for (int i=;i<sz;i++){
Edge& e=edges[G[now][i]];
if (!vis[G[now][i]] && dis[now]==dis[e.to]+e.cost){
vis[G[now][i]]=;
vis[G[now][i]^]=;
f[now]=(f[now]+work(e.to))%MOD;
}
}
return f[now];
} int main(){
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++) dis[i]=INF;
for (int i=;i<m;i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
AddEdge(x,y,z);
}
SPFA();
//printf("%d\n",dis[n]);
memset(vis,,sizeof(vis));
if (!check(n)){
printf("-1\n");
return ;
}
memset(vis,,sizeof(vis));
memset(f,-,sizeof(f));
f[]=;
printf("%d\n",work(n));
return ;
}
/*
4 4
1 2 1
1 3 1
2 4 2
3 4 2 4 4
1 2 0
1 3 1
2 4 99
3 4 99
*/
cdoj 秋实大哥带我飞 最短路走法 含0权边的更多相关文章
- UESTC_秋实大哥带我飞 2015 UESTC Training for Graph Theory<Problem B>
B - 秋实大哥带我飞 Time Limit: 300/100MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit ...
- cdoj 秋实大哥搞算数
地址:http://acm.uestc.edu.cn/#/contest/show/95 题目: N - 秋实大哥搞算数 Time Limit: 3000/1000MS (Java/Others) ...
- cdoj 秋实大哥与战争
首先,显然每个区间的最长连续子区间要么在左孩子里,要么在右孩子里,要么跨越两个孩子.于是我们可以对每个区间维护如下信息ll(left long),rl(rigth long),ml(mid long) ...
- CDOJ 1146 A - 秋实大哥与连锁快餐店 最小生成树 Prim算法 稠密图
题目链接 A - 秋实大哥与连锁快餐店 Time Limit:3000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu S ...
- CDOJ 1070 秋实大哥打游戏 带权并查集
链接 F - 秋实大哥打游戏 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit ...
- CDOJ 1069 秋实大哥去打工 单调栈 下标处理
E - 秋实大哥去打工 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit St ...
- CDOJ 1061 C - 秋实大哥与战争 STL set 迭代器
题目链接: C - 秋实大哥与战争 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Sub ...
- CDOJ 1060 秋实大哥与快餐店 字典树 水题
题目链接 B - 秋实大哥与快餐店 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Sub ...
- CDOJ 1057 秋实大哥与花 线段树 区间更新+区间查询
链接: I - 秋实大哥与花 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit ...
随机推荐
- first blood暴力搜索,剪枝是关键
First Blood 题目描述 盖伦是个小学一年级的学生,在一次数学课的时候,老师给他们出了一个难题: 老师给了一个正整数 n,需要在不大于n的范围内选择三个正整数(可以是相同的),使它们三个的最小 ...
- C语言基础11
函数指针的定义: 函数类型 (标识符 指针变量名)(形参列表) void printHello( ); void printHello( ){ printf("hello world!!! ...
- linux----用户与whoami
linux中的su 命令可以完成用户切换:如我们先由root切换到mysql用户可以这样做su - mysql whoami #这个时候linux会打印出mysql who am i #这个时候lin ...
- Oracle EBS-SQL (INV-11):检查子库存会计信息.sql
select OOD.ORGANIZATION_CODE 库存组织代码, ...
- EasyUI两种动态添加tab Iframe页面的方法
/** 动态添加tab-----方式一 **/ function addIframeTab(titleTxt,href,icon) { $('#mytabs').tabs('addIframeTab' ...
- linux之SQL语句简明教程---SELECT
SQL是用来做什么的呢?一个最常用的方式是将资料从数据库中的表格内选出.从这一句回答中,我们马上可以看到两个关键字: 从 (FROM) 数据库中的表格内 选出 (SELECT).(表格是一个数据库内的 ...
- leetcode Permutation
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- java对象转json应clone,避免生成json串有问题
今天因为一个java对象转json,搞了我一下午,在些记录一下: 是这样:我在strtuts2的action中调用services返回 Row: 26, 中国银行海鹰, 29, 东楼, 36, 1F ...
- Oracle表空间常用操作
--创建表空间 create tablespace test datafile 'E:\test2_data.dbf' SIZE 20M autoextend on next 5M maxsize 5 ...
- 3、使用Lucene实现千度搜索
1.新建Web项目 新建一个Web项目,我命名为SearchEngine,然后导入Java包: 除了上篇博客中的Jar包外,我还引入了 IKAnalyzer2012_FF.jar 包和struts2的 ...