uva10817 dijkstra

大白书P330

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 1000+10;
const int INF = 1000000000;
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;
   vector<Edge> edges;
   vector<int>G[maxn];
   bool done[maxn];
   int d[maxn];
   int p[maxn];
   void inti(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});
       int 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, false,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});
              }
          }
      }
   }
}solve1;
vector<int> R[maxn];
int dp[maxn];
int dfs(int u){
    if(dp[u]!=-1)  return dp[u];
    if(u==1) return 1;
    dp[u]=0;
    for(int i =0 ; i<R[u].size(); i++){
         int v = R[u][i];
         dp[u]+=dfs(v);
    }
    return dp[u];
}
int main()
{
    int n,m;
    while(scanf("%d",&n)==1&&n){
         scanf("%d",&m);
         solve1.inti(n);
         for(int i = 0; i<m; ++i){
              int u,v,d;
              scanf("%d%d%d",&u,&v,&d); u--,v--;
              solve1.AddEdge(u,v,d);
              solve1.AddEdge(v,u,d);
         }
         solve1.dijkstra(1);
         for(int i = 0; i<n; i++) R[i].clear();
         for(int i = 0; i < solve1.edges.size(); ++i){
               int u=solve1.edges[i].from, v=solve1.edges[i].to;
               if(solve1.d[u]>solve1.d[v])
                 R[u].push_back(v);
         }
         memset(dp,-1,sizeof(dp));
         printf("%d\n",dfs(0));
    }
    return 0;
}

时间: 2024-08-28 13:49:20

uva10817 dijkstra的相关文章

畅通project续HDU杭电1874【dijkstra算法 || SPFA】

http://acm.hdu.edu.cn/showproblem.php?pid=1874 Problem Description 某省自从实行了非常多年的畅通project计划后.最终修建了非常多路.只是路多了也不好,每次要从一个城镇到还有一个城镇时,都有很多种道路方案能够选择,而某些方案要比还有一些方案行走的距离要短非常多.这让行人非常困扰. 如今,已知起点和终点,请你计算出要从起点到终点.最短须要行走多少距离. Input 本题目包括多组数据.请处理到文件结束. 每组数据第一行包括两个正

Choose the best route 【Dijkstra】

Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend's home as soon as possible . Now give you a map of the city's traffic route, and the stations which are near Kiki

Bus System 【dijkstra算法】

Because of the huge population of China, public transportation is very important. Bus is an important transportation method in traditional public transportation system. And it's still playing an important role even now.The bus system of City X is qui

Dijkstra

//在这里采用两种方式实现Dijkstra算法,经过测试两种方式:第一种:通过Dist[]让每一个顶点中Dist[]中寻找最短路径的点:第二种方式:从已经收录的顶点的相邻顶点中选择出最短路//径的的顶点,此种方式适合用于稠密图,减少点的遍历. #include<iostream> #include<malloc.h> using namespace std; #define INF (100000) //相连顶点的信息 typedef struct AdjNode{ int Wei

ACM: HDU 2544 最短路-Dijkstra算法

HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗? Input 输入包括多组数据.每组数据第一行是两个整数N.M(N<=100,M<

ACM: HDU 3790 最短路径问题-Dijkstra算法

HDU 3790 最短路径问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的. Input 输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p.最后一行是

dijkstra堆优化模板

1 #include<iostream> 2 #include<cmath> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdio> 6 #include<queue> 7 #define inf 2147483647 8 using namespace std; 9 struct data 10 { 11 int from,to,next,w; 12 data(){f

邻接表实现Dijkstra算法以及DFS与BFS算法

//============================================================================ // Name : ListDijkstra.cpp // Author : fffff // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //==========================

Dijkstra算法(求解单源最短路)详解 + 变形 之 poj 1860 Currency Exchange

/* 求解单源最短路问题:Dijkstra算法(该图所有边的权值非负) 关键(贪心): (1)找到最短距离已经确定的节点,从它出发更新与其相邻节点的最短距离: (2)此后不再关心(1)中“最短距离已经确定的节点”. 时间复杂度(大概的分析,不准确): “找到最短距离已经确定的节点” => O(|V|) "从它出发更新与其相邻节点的最短距离" => 邻接矩阵:O(|V|),邻接表:O(|E|) 需要循环以上两个步骤V次,所以时间复杂度:O(V^2) 即:在|E|较小的情况下,