dijkstra模板

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
#define maxedge 20000
#define maxn 5000     //结点数
#define inf 1<<28
bool vist[maxn];
struct Edge
{
    int s;   //边的起点
    int t;   //边的终点
    int next;//当前下一条边的编号
    int w;  //边的权值
}edge[maxedge];
int headlist[maxedge]; //索引  head[i]存放已i为起点的“第一条”边
int distance[maxn];
int n,m,x;
void dij()
{
    int i,j,k;
    memset(vist,false,sizeof(vist));
    for(i=1;i<=n;++i)
     distance[i]=inf;
     distance[x]=0;
    for(i=1;i<=n;i++)
    {   k=-1;
        for(j=1;j<=n;j++)
          if(!vist[j]&&(k==-1||distance[k]>distance[j]))
            k=j;
        vist[k]=true;
        for(j=headlist[k];j!=-1;j=edge[j].next)
        if(!vist[edge[j].t]&&(distance[k]+edge[j].w<distance[edge[j].t]))
            distance[edge[j].t]=distance[k]+edge[j].w;
    }
    for(i=1;i<=n;i++)
    printf("%d ",distance[i]);
}
int main()
{
    int i,a,b,c;
    while(~scanf("%d%d%d",&n,&m,&x))
    {
        for(i=1;i<=n;++i)
          headlist[i]=-1;
        for(i=1;i<=m;++i)
        {
            scanf("%d%d%d",&a,&b,&c);
            edge[i].s=a;
            edge[i].t=b;
            edge[i].w=c;
            edge[i].next=headlist[a];//索引:节点i 后一条边编号为headlist[a];
            headlist[a]=i;

        }
        dij();
    }

    return 0;
}
时间: 2024-10-29 19:11:50

dijkstra模板的相关文章

Dijkstra模板题图论书p133

#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <

UVA - 11374 Airport Express (Dijkstra模板+枚举)

Description Problem D: Airport Express In a small city called Iokh, a train service, Airport-Express, takes residents to the airport more quickly than other transports. There are two types of trains in Airport-Express, the Economy-Xpress and the Comm

最短路径之 Dijkstra模板

一:时间复杂度为O(V*V)的Dijkstra const int Max_v = 100 + 10; const int INF = 1<<30; int cost[Max_v][Max_v];//权值 int d[Max_v];//顶点s出发最短距离 bool used[Max_v];//以使用过的图 int V;//顶点数 int Edge;//边数 void dijkstra(int s) { fill(d,d+V,INF); fill(used,used+V,false); d[s]

Dijkstra 模板 最短路

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents ------------------------------------------------------------------------------------------------------------------------------------------------------ 欢迎光临天资小屋:http://user.qzone.qq.com/5938309

朴素Dijkstra模板函数

朴素Dijkstra不需要用到堆,每次找最小值都得遍历一遍,时间复杂度较高. 点是从编号0到编号n-1的n个点. 以下是模板: int Dijkstra(int a,int b) //a是起点,b是终点 { bool visited[maxn]; // 记录访问点 int pos = a, min, dist[maxn]; // pos是个标记点,标记每次最小边对应的点 ,dist记录起点到各点间最短距离. for(int i = 0; i < n; i++) { dist[i] = map[a

最短路径 一 Dijkstra 模板(O(n^2))

题目传送:http://hihocoder.com/problemset/problem/1081 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define INF 0x03F3F3F3F 5 #define N 1024 6 int path[N], vis[N]; 7 int cost[N][N],lowcost[N]; 8 void Dijkstra(int n, int beg){

HDU 2544最短路dijkstra模板题

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 33657    Accepted Submission(s): 14617 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找

hdu 2544 hdu 1874 Dijkstra 模板题

hdu 2544  求点1到点n的最短路 Sample Input2 1 //结点数 边数1 2 3 //u v w3 31 2 52 3 53 1 20 0 Sample Output32 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # define LL long

hdu 2544 最短路 dijkstra模板

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 38804    Accepted Submission(s): 16925 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最

POJ 2387 Til the Cows Come Home (dijkstra模板题)

Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible. Farmer Joh