layout: post

title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP)

author: "luowentaoaa"

catalog: true

mathjax: true

tags:

- 最短路

- 基础DP

- Dijkstra

- 图论

- 训练指南


Walk Through the Forest UVA - 10917

题意

Jimmy打算每天沿着一条不同的路走,而且,他只能沿着满足如下条件的道路(A,B):存在一条从B出发回家的路径,比所以从A出发回家的路径都短,你的任务是计算有多少条不同的路径

题意

题意就转化成如果终点到i 比到j的路劲短,就连线,然后记忆化搜索就行(这几天这种题做太多次了)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=1050;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
struct Edge{
int from,to,dist;
};
struct HeapNode{
int d,u;
bool operator <(const HeapNode& rhs)const{
return d>rhs.d;
}
};
struct Dijkstra{
int n,m; ///点数和边数 点编号0~N-1
vector<Edge>edges; ///边列表
vector<int>G[maxn]; ///每个节点出发的边编号
bool done[maxn]; /// 是否已永久标号
int d[maxn]; /// s到各个点的距离
int p[maxn]; /// 最短路中的上一条边 void init(int n){
this->n=n;
for(int i=0;i<n;i++)G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int dist){ ///无向图调用两次
edges.push_back((Edge){from,to,dist});
m=edges.size();
G[from].push_back(m-1);
}
void dijkstra(int s){
priority_queue<HeapNode>Q;
for(int i=0;i<n;i++)d[i]=inf;
d[s]=0;
memset(done,0,sizeof(done));
Q.push((HeapNode){0,s});
while(!Q.empty()){
HeapNode x=Q.top();Q.pop();
int u=x.u;
if(done[u])continue;
done[u]=true;
for(int i=0;i<G[u].size();i++){
Edge& e=edges[G[u][i]];
if(d[e.to]>d[u]+e.dist){
d[e.to]=d[u]+e.dist;
p[e.to]=G[u][i];
Q.push((HeapNode){d[e.to],e.to});
}
}
}
}
/// dist[i]为s到i的距离,paths[i]为s到i的最短路径(经过的结点列表,包括s和t)
void GetShortestPaths(int s,int* dist,vector<int>* paths){///paths是二维链表
dijkstra(s);
for(int i=0;i<n;i++){
dist[i]=d[i];
paths[i].clear();
int t=i;
paths[i].push_back(t);
while(t!=s){
paths[i].push_back(edges[p[t]].from);
t=edges[p[t]].from;
}
reverse(paths[i].begin(),paths[i].end());
}
}
}; Dijkstra solver;
int d[maxn];
int dp(int u){
if(u==1)return 1;
int &ans=d[u];
if(ans>=0)return ans;
ans=0;
for(int i=0;i<solver.G[u].size();i++){
int v=solver.edges[solver.G[u][i]].to;
if(solver.d[v]<solver.d[u])ans+=dp(v);
}
return ans;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int n,m;
while(cin>>n){
if(n==0)break;
cin>>m;
solver.init(n);
for(int i=0;i<m;i++){
int a,b,c;
cin>>a>>b>>c;a--;b--;
solver.AddEdge(a,b,c);
solver.AddEdge(b,a,c);
}
solver.dijkstra(1);
memset(d,-1,sizeof(d));
cout<<dp(0)<<endl;
}
return 0;
}

训练指南 UVA - 10917(最短路Dijkstra + 基础DP)的更多相关文章

  1. 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)

    layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...

  2. 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环)

    layout: post title: 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环) author: "luowentaoaa" catalog: ...

  3. 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束)

    layout: post title: 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束) author: "luowentaoaa" catal ...

  4. 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)

    layout: post title: 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP) author: "luowentaoaa" catalog: true ...

  5. 训练指南 UVA - 11419(二分图最小覆盖数)

    layout: post title: 训练指南 UVA - 11419(二分图最小覆盖数) author: "luowentaoaa" catalog: true mathjax ...

  6. 训练指南 UVA - 11383(KM算法的应用 lx+ly >=w(x,y))

    layout: post title: 训练指南 UVA - 11383(KM算法的应用 lx+ly >=w(x,y)) author: "luowentaoaa" cata ...

  7. 训练指南 UVA - 11354(最小生成树 + 倍增LCA)

    layout: post title: 训练指南 UVA - 11354(最小生成树 + 倍增LCA) author: "luowentaoaa" catalog: true ma ...

  8. 算法竞赛入门经典训练指南——UVA 11300 preading the Wealth

    A Communist regime is trying to redistribute wealth in a village. They have have decided to sit ever ...

  9. uva 10917 最短路+dp

    https://vjudge.net/problem/UVA-10917 给出N点M边的无向图,没重边.对于点A,B,当且仅当从B到终点的最短路小于任何一条从A到终点的最短路时,才考虑从A走到B,否则 ...

随机推荐

  1. AGC018C Coins (set)

    题目大意: 给出n个人,每个人手里都有xi个金牌,yi个银牌,ci个铜牌. 你需要选出X个人,拿走他们手里的金牌,选出Y个人,拿走他们手里的银牌,选出Z个人,拿走他们手里的铜牌 X+Y+Z = n.并 ...

  2. [洛谷P3765]总统选举

    题目大意:有$n(n\leqslant5\times10^5)$个数,有$m(m\leqslant5\times10^5)$次询问. 一次询问形如$l\;r\;s\;k\;w_1\;w_2\dots ...

  3. AtCoder Grand Contest 028 B - Removing Blocks 解题报告

    B - Removing Blocks Time limit : 2sec / Memory limit : 1024MB Score : 600 points ## Problem Statemen ...

  4. centos关闭ipv6

    1.使用lsmod查看ipv6的模块是否被加载. lsmod | grep ipv6 [root@dmhadoop011 ~]# lsmod | grep ipv6 ipv6              ...

  5. BootStrap弹出框插件popover简单实例

    1.网上实例地址 http://www.runoob.com/bootstrap/bootstrap-popover-plugin.html 2.具体demo     $("#pieId&q ...

  6. bzoj2453/2120 数颜色

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2120 http://www.lydsy.com/JudgeOnline/problem.ph ...

  7. mongoDB的文档查询

    1.简单查询: find() 方法以非结构化的方式来显示所有文档. 语法 MongoDB 查询数据的语法格式如下:      collection是集合名字,注意应该是当前数据库的集合,collect ...

  8. LCD实验学习笔记(一):Makefile

    主Makefile总领全局的就这句—— lcd.bin: $(objs) 要生成lcd.bin,依赖于objs列举的一堆文件:head.o init.o nand.o interrupt.o seri ...

  9. [转]在树莓派上搭建LAMP服务

    之前介绍过树莓派上LNMP环境的搭建方法,本文将详细介绍如何在树莓派上配置LAMP服务. 为LAMP,是最流行的服务器配置之一,LAMP的含义是: Linux - 操作系统 Apache - 网络服务 ...

  10. HDU2594(简单KMP)

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...