//Dijkstra-感觉和prim很像,就是很像
#include
#include
#include
#define MAX 0x3f3f3f
int map[150][150];
int dis[150];
int n;
int bj[150];
void dijkstra()
{
memset(bj,0,sizeof(bj));
memset(dis,0,sizeof(dis));
int pos = 1,i,j;
for(i = 1; i <= n; i++)
dis[i] = map[pos][i];
bj[1] = 1;
for(i = 1; i < n; i++)
{
int min = MAX;
for(j = 1; j <= n; j++)
{
if(min > dis[j] && !bj[j])
min = dis[pos = j];
}
bj[pos] = 1;
for(j = 1; j <= n; j++)
{
if(dis[j] > map[pos][j]+dis[pos]&& !bj[j])
dis[j] = map[pos][j]+dis[pos];
}
}
printf("%d\n",dis[n]);
}
int main()
{
int m;
while(~scanf("%d%d",&n,&m))
{
int i, j;
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
if(i!=j)
map[i][j] = MAX;
int u, v, w;
for(i = 0;i < m; i++)
{
scanf("%d%d%d",&u,&v,&w);
if(map[u][v]>w)
{
map[u][v] = w;
map[v][u] = w;
}
}
dijkstra();
}
return 0;
}
Bellman-ford,和krusal感觉很像,也可能是错觉
#include
#include
#include
#define MAX 0x3f3f3f
int n,m, cont;
struct node
{
int u,v,w;
}ls[15000];
void BF()
{
int dis[200];
for(int i = 0; i <= n; i++)
dis[i] = MAX;
dis[1] = 0;
for(int i = 1; i < n; i++)
for(int j = 0; j < cont; j++)
{
int a = ls[j].u, b = ls[j].v;
if(dis[b] > dis[a] + ls[j].w)
dis[b] = dis[a] + ls[j].w;
}
printf("%d\n",dis[n]);
}
int main()
{
while(~scanf("%d%d",&n,&m)&&(n+m))
{
int i;
cont = 0;
for(i = 0; i < m; i++)
{
int u, v, w;
scanf("%d%d%d",&u,&v,&w);
int flag = 0;
for(int j = 0; j < cont; j++)
{
if((ls[j].u == u && ls[j].v == v)||(ls[j].u == v && ls[j].v == u))
if(ls[j].w > w)
{
ls[j].w = w;
flag = 1;
}
}
if(!flag)
{
ls[cont].u = u;
ls[cont].v = v;
ls[cont++].w = w;
ls[cont].u = v;
ls[cont].v = u;
ls[cont++].w = w;
}
}
BF();
}
return 0;
}
#include
#include
#include
#define MAX 0x3f3f3f
int n,m;
int map[200][200];
void Floyd()
{
int i, j, k;
for(i = 0;i <= n; i++)
for(j = 0; j <= n; j++)
for(k = 0; k <= n; k++)
if(map[k][i] + map[i][j] < map[k][j])
map[k][j] = map[k][i]+map[i][j];
printf("%d\n",map[1][n]);
}
int main()
{
while(~scanf("%d%d",&n,&m)&&(n+m))
{
int i,j;
for(i = 0; i <= n; i++)
for(j = 0; j <= n; j++)
if(i != j)
map[i][j] = MAX;
for(i = 0; i < m; i++)
{
int u, v, w;
scanf("%d%d%d",&u,&v,&w);
if(map[u][v] > w)
{
map[u][v] = w;
map[v][u] = w;
}
}
Floyd();
}
return 0;
}
SPFA 是在BF上优化的,加了队列,
#include
#include
#include
#include
#include
#include
#define Maxn 150
#define Maxm 10000
#define Max 10000
using namespace std;
int head[Maxm],bj[Maxn],dis[Maxn];
int pos[Maxn];
int n,m;
struct node
{
int v, w, next;
}ls[20000];
bool SPFA(int start)
{
queuea;
for(int i = 0; i <= n; i++)
dis[i] = 9999999;
bj[start] = 1;
dis[start] = 0;
a.push(start);
while(!a.empty())
{
int top = a.front();
a.pop();
pos[top]++;
bj[top] = 0;
if(pos[top] > n) return false;
for(int i = head[top]; i!=-1;i = ls[i].next)
{
if(dis[ls[i].v] > dis[top] + ls[i].w)
{
dis[ls[i].v] = dis[top] + ls[i].w;
if(!bj[ls[i].v])
{
bj[ls[i].v] = 1;
a.push(ls[i].v);
}
}
}
}
return true;
}
int main()
{
while(~scanf("%d%d",&n,&m)&&(n+m))
{
memset(head,-1,sizeof(head));
memset(pos,0,sizeof(pos));
int k = 0;
while(m--)
{
int u, v, w;
scanf("%d%d%d",&u,&v,&w);
ls[k].v = v;
ls[k].w = w;
ls[k].next = head[u];
head[u] = k++;
ls[k].v = u;
ls[k].w = w;
ls[k].next = head[v];
head[v] = k++;
}
if(SPFA(1))
printf("%d\n",dis[n]);
}
return 0;
}