这道题很是说明了记忆化搜索的重要性

瞎bfs递推半天发现没卵用(也许是姿势不对,但我认为树形或图形dfs明显好写并且很好正确地递推)

参考了别人的写法,总感觉自己的实现能力太弱了

还有题目是1e9+9,送了3WA(一眼1e9+7hhhhh)

/*H E A D*/
int to[maxn<<1],nxt[maxn<<1],cost[maxn<<1],head[maxn],tot;
void init(){
memset(head,-1,sizeof head);
tot=0;
}
void add(int u,int v,int w){
to[tot]=v;cost[tot]=w;nxt[tot]=head[u];head[u]=tot++;
swap(u,v);
to[tot]=v;cost[tot]=w;nxt[tot]=head[u];head[u]=tot++;
}
int dp[maxn];
int dis[maxn];
int n,m;
typedef pair<int,int> P;
void dijkstra(int s){
memset(dis,oo,sizeof dis);
priority_queue<P,vector<P>,greater<P> > que;
que.push(P(s,0));
dis[s]=0; dp[s]=1;
while(!que.empty()){
P p=que.top(); que.pop();
int u=p.first;
if(dis[u]<p.second)continue;
erep(i,u){
int v=to[i],w=cost[i];
if(dis[v]>dis[u]+w){
dis[v]=dis[u]+w;
que.push(P(v,dis[v]));
}
}
}
}
bool vis[maxn];
bool findzero(int u){
if(u==1)return 0;
bool flag=0;
erep(i,u){
int v=to[i],w=cost[i];
if(!vis[i]&&dis[u]==dis[v]+w){
vis[i]=vis[i^1]=1;
if(w==0) return 1;
if(findzero(v)) return 1;
}
}
return 0;
}
int DP(int u){
if(~dp[u]) return dp[u];
dp[u]=0;
erep(i,u){
int v=to[i],w=cost[i];
if(!vis[i]&&dis[u]==dis[v]+w){
vis[i]=1;
vis[i^1]=1;
dp[u]=(1ll*dp[u]+DP(v))%mod;
}
}
return dp[u];
}
int main(){
while(cin>>n>>m){
init();
rep(i,1,m){
int u=read();
int v=read();
int w=read();
add(u,v,w);
}
dijkstra(1);
memset(vis,0,sizeof vis);
bool flag=findzero(n);
if(flag) println(-1);
else{
memset(dp,-1,sizeof dp);
memset(vis,0,sizeof vis);
dp[1]=1;
println(DP(n));
}
}
return 0;
}

一组测试用数据

4 5
1 2 2
1 3 1
2 3 1
2 4 1
3 4 2

ans:3

顺便挂一下看着不错的写法:http://blog.csdn.net/code12hour/article/details/52081457

UESTC - 1147 求最短路方案数的更多相关文章

  1. HDU 1688 Sightseeing&HDU 3191 How Many Paths Are There(Dijkstra变形求次短路条数)

    Sightseeing Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  2. Codeforces 509F Progress Monitoring:区间dp【根据遍历顺序求树的方案数】

    题目链接:http://codeforces.com/problemset/problem/509/F 题意: 告诉你遍历一棵树的方法,以及遍历节点的顺序a[i],长度为n. 问你这棵树有多少种可能的 ...

  3. P1466 集合 Subset Sums(01背包求填充方案数)

    题目链接:https://www.luogu.org/problem/show?pid=1466 题目大意:对于从1到N (1 <= N <= 39) 的连续整数集合,能划分成两个子集合, ...

  4. caioj 1412 动态规划3:a+b问题(完全背包方案数)

    每个素数就是一个物品,然后就相当于求完全背包方案数 把max改成+就好了. #include<cstdio> #include<vector> #include<cstr ...

  5. HDU 3416 Marriage Match IV (求最短路的条数,最大流)

    Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...

  6. poj 3734 方块涂色 求红色 绿色方块都为偶数的方案数 (矩阵快速幂)

    N个方块排成一列 用红,蓝,绿,黄4种颜色去涂色,求红色方块 和绿色方块个数同时为偶数的 方案数 对10007取余 Sample Input 212Sample Output 2//(蓝,黄)6//( ...

  7. P1474 货币系统 Money Systems(完全背包求填充方案数)

    题目链接:https://www.luogu.org/problemnew/show/1474 题目大意:有V种货币,求用V种货币凑出面值N有多少种方案. 解题思路:就是完全背包问题,只是将求最大价值 ...

  8. poj3254 Corn Fields 利用状态压缩求方案数;

    Corn Fields 2015-11-25 13:42:33 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10658   ...

  9. CF 149D Coloring Brackets(区间DP,好题,给配对的括号上色,求上色方案数,限制条件多,dp四维)

    1.http://codeforces.com/problemset/problem/149/D 2.题目大意 给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色, ...

随机推荐

  1. Mpich编程

    一.简介 通过安装MPICH构建MPI编程环境,从而进行并行程序的开发.MPICH是MPI(Message-Passing Interface)的一个应用实现,支持最新的MPI-2接口标准,是用于并行 ...

  2. ubuntu开启ssh

    SSH分客户端openssh-client和openssh-server如果你只是想登陆别的机器的SSH只需要安装openssh-client(ubuntu有默认安装,如果没有则sudo apt-ge ...

  3. SQLAlchemy(ORM框架)

    SQLAlchemy SQLAlchemy概述 2 3 4 5 6 7 8 9 10 11 12 13 MySQL-Python     mysql+mysqldb://<user>:&l ...

  4. Yii2视频

    Yii2 视频分享 需要的小伙伴看过来链接: https://pan.baidu.com/s/1sl4H0RV 密码: nknx  (有问题请留言)

  5. Django框架 之 中间件

    Django框架 之 中间件 浏览目录 中间件介绍 自定义中间件 中间件的执行流程 中间件版登录验证 一.中间件介绍 官方的说法:中间件是一个用来处理Django的请求和响应的框架级别的钩子.它是一个 ...

  6. Python基础-4

    目录 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 1.列表生成式,迭代器&生成器 看列表[0, 1, 2, 3, 4, 5, 6, 7, ...

  7. python核心编程第2章课后题答案(第二版36页)

    2-5 Loops and Numbers a) i = 0    while i <11:     print i    i += 1 b) for i in range(0,11): pri ...

  8. 第02章-装配Bean

    1. Spring配置的可选方案 在XML中进行显式配置: 在Java中进行显式配置: 隐式的bean发现机制和自动装配. 2. 自动化装配bean Spring从两个角度来实现自动化装配: 组件扫描 ...

  9. Mathematical optimization数学上的最优化

    https://en.wikipedia.org/wiki/Mathematical_optimization In mathematics, computer science and operati ...

  10. LIRE教程之源码分析 | LIRE Tutorial of Analysis of the Source Code

    LIRE教程之源码分析 |LIRE Tutorial of Analysis of the Source Code 最近在做地理图像识别和检索的研究,发现了一个很好用的框架LIRE,遂研究了一通.网上 ...