最短路

最短路算法

Floyd

SPFA

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;

const int maxn=103;
const int maxm=23;
const int maxe=maxm*maxm*2;
const int INF=2e9;

int n,m,k,e,x,y,z,d,p,a,b;
int tot,point[maxm],nxt[maxe],v[maxe],c[maxe];
int dis[maxm];
bool vis[maxm],flag[maxm];
queue <int> q;

inline void addedge(int x,int y,int z)
{
    ++tot;
    nxt[tot]=point[x];
    point[x]=tot;
    v[tot]=y;
    c[tot]=z;
    ++tot;
    nxt[tot]=point[y];
    point[y]=tot;
    v[tot]=x;
    c[tot]=z;
}
inline int spfa()
{
    memset(dis,0x7f,sizeof(dis));
    dis[1]=0;
    memset(vis,0,sizeof(vis));
    vis[1]=true;
    while (!q.empty()) q.pop();
    q.push(1);

    while (!q.empty())
    {
        int now=q.front();
        q.pop();
        vis[now]=false;
        for (int i=point[now]; i; i=nxt[i])
            if (dis[v[i]]>dis[now]+c[i]&&!flag[v[i]])
            {
                dis[v[i]]=dis[now]+c[i];
                if (!vis[v[i]])
                {
                    vis[v[i]]=true;
                    q.push(v[i]);
                }
            }
    }
    return dis[m];
}

Dijkstra

例题:hdu2544

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//Dijkstra
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<queue>
using namespace std;
template<typename T>
void Swap(T& a,T& b){T t=a;a=b;b=t;}
template<typename T>
int read(T& w)
{
	char r=getchar();bool f=false;
	if(r==-1)return -1;
	for(;(r<48||r>57)&&r!='-';r=getchar());
	if(r=='-')r=getchar(),f=true;
	for(w=0;r>=48&&r<=57;r=getchar())w=w*10+r-48;
	if(f)w=-w;
	return 1;
}
const int maxn=103;
const int maxm=10003;
int n,m;
struct EDGE
{
	int to,nxt,w;
	void init(int too,int nxtt,int ww)
	{
		to=too;nxt=nxtt;w=ww;
	}
}edge[maxm<<1];
int ek=0;
int node[maxn];
struct p
{
	int d,id;
	p(){d=id=0;}
	p(int idd,int dd)
	{
		d=dd,id=idd;
	}
};
void addEdge(int from,int too,int ww)
{
	edge[++ek].init(too,node[from],ww);
	node[from]=ek;
}
bool operator<(const p& a,const p& b)
{
	return a.d<b.d;
}
int to[maxn]; 
//小根堆
const int maxnode=maxn;
template<typename T>
struct HEAP
{
	T ary[maxnode];
	int f;
	HEAP(){f=0;}
	void clear(){f=0;}
	bool empty(){return f==0;}
	void push(const T& w)
	{
		ary[++f]=w;
		for(int k=f;k!=1&&ary[k]<ary[k/2];k/=2)
		{
			Swap(ary[k],ary[k/2]);
			Swap(to[ary[k].id],to[ary[k/2].id]);
		}
	}
	void adjust(int k,int dd)
	{
		if(dd<ary[k].d)ary[k].d=dd;
		for(;k!=1&&ary[k]<ary[k/2];k/=2)
		{
			Swap(ary[k],ary[k/2]);
			Swap(to[ary[k].id],to[ary[k/2].id]);
		}
	}
	T top() {return ary[1];}
	void pop()
	{
		ary[1]=ary[f--];
		for(int k=1,son=k*2;son<=f;k=son,son=k*2)
		{
			if(son+1<=f&&ary[son+1]<ary[son])
				++son;
			if(ary[son]<ary[k])
				Swap(ary[son],ary[k]),
				Swap(to[ary[son].id],to[ary[k].id]);
			else break;
		}
	}
};
HEAP<p> q;
int s,t;
typedef unsigned int uint;
const uint uinf=4294967295u;
uint d[maxn];
bool ok[maxn];

void Dijkstra()
{
	memset(d+1,255,n*sizeof(uint));
	memset(ok+1,0,n*sizeof(bool));
	memset(to+1,0,n*sizeof(int));
	q.clear();
	if(!to[s])
		to[s]=q.f+1;
	q.push(p(s,0));
	for(int i=1;i<=n&&!q.empty();++i)
	{
		p tmp;
		do
		{	tmp=q.top();
			q.pop();
			to[tmp.id]=0;
		}while(ok[tmp.id]&&!q.empty());
		ok[tmp.id]=true;
		d[tmp.id]=tmp.d;
		for(int j=node[tmp.id];j;j=edge[j].nxt)
		{
			int v=edge[j].to;
			if(d[tmp.id]!=uinf&&!ok[v])
			if(d[tmp.id]+edge[j].w<d[v])
			{
				if(!to[v])
					to[v]=q.f+1,q.push(p(v,d[tmp.id]+edge[j].w));
				else q.adjust(to[v],d[tmp.id]+edge[j].w);			
			}
		}
		if(tmp.id==t)break;
	}
}
void input()
{
	ek=0;
	memset(node+1,0,n*sizeof(int));
	int x,y,l;
	for(int i=1;i<=m;++i)
	{
		read(x),read(y),read(l);
		if(x!=y)
		{
			addEdge(x,y,l);
			addEdge(y,x,l);
		}
	}
	s=1,t=n;
}
int main()
{
	freopen("hdu2544_1.in","r",stdin);
	//freopen("hdu2544.out","w",stdout);
	while(read(n),read(m),n!=0)
	{
		input();
		Dijkstra();
		printf("%u\n",d[t]);
	}
	return 0;
}

更多题目:

0%