shortest path

1. Dijkstra algorithm template:

//dijkstra
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define INF 0x3f3f3f
#define max 1000+10
using namespace std;
int visit[max],map[max][max],dist[max],time[max];
int n,m;
int start;
void dijkstra(int start)
{
    int i,j,next;
    int mindist;
    for(i=1;i<=n;i++)
    {
        visit[i]=0;
        dist[i]=map[start][i];
    }
    visit[start]=1;
    for(i=2;i<=n;i++)
    {
        next=i;
        mindist = INF;
        for(j=1;j<=n;j++)
        {
            if(!visit[j]&&mindist>dist[j])
            {
                mindist=dist[j];
                next=j;
            }
        }
        visit[next]=1;
        for(j=1;j<=n;j++)
        {
            if(!visit[j]&&dist[next]+map[next][j]<dist[j])
            dist[j]=dist[next]+map[next][j];
        }
    }
}
intmain()
{
    int i,j,x,y,c,road,end;
    while(scanf("%d%d%d",&n,&m,&start)!=EOF)
    {
        memset(time,INF,sizeof(time));
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(i==j)
                map[i][j]=0;
                else
                map[i][j]=INF;
            }
        }
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&c);
            if(map[y][x]>c)
            map[y][x]=c;
        }
        dijkstra(start);
        scanf("%d",&road);
        for(i=0;i<road;i++)
        {
            scanf("%d",&end);
            time[i]=dist[end];
        }
        sort(time,time+road);
        if(time[0]==INF)
        printf("-1\n");
        else
        printf("%d\n",time[0]);
    }
    return 0;
}
2. The topic template

1. City calm

Question: It is known that there is a rebellion in a city, and there are N troops to maintain the law and order of M cities. Knowing the time when cities reach each other, N troops are distributed in any N cities, and ask how long it takes for troops to rescue a city after a riot.

Idea: Use Dijkstra's algorithm, take the riot location as the starting point, find the shortest time to reach each location, and then compare all the shortest ones.

Code: https://blog.csdn.net/chenzhenyu123456/article/details/43973471

2.find the safest road

Question: Knowing a matrix representing the safety factor of urban construction, the safety of a channel P from u to v is Safe(P) = s(e1)*s(e2)…*s(ek) e1,e2,ek is an edge on P that outputs the safety factor of the safest road between the two cities.

Idea: template question of dijkstra algorithm, but pay attention to  dist[j]=dist[next]*map[next][j]; , and then double the whole process

Code: https://blog.csdn.net/chenzhenyu123456/article/details/44118045

3. Shortest path + minimum cost problem

Question: Give you n points, m undirected edges, each edge has length d and cost p, give you starting point s and end point t, ask to output the shortest distance from the starting point to the end point and its cost, if there are multiple shortest distances route, the output costs the least.

Idea: Change the template directly in the dijkstra algorithm, when dist[next]+map[next][j]==dist[j]&&money[next]+cost[next][j]<money[j], do money[j]=money[next]+cost[next][j]; operation. Then~

Code: https://blog.csdn.net/chenzhenyu123456/article/details/44247177

4. For the data that is too large, the map cannot be stored, you can use the linked list form

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
const int MAXN = 200009;
int n, m, tot, head[MAXN];
bool vis[MAXN];
ll f[MAXN];
struct Edge
{
	int to, w, nex;
}edge[MAXN * 2];
void init()
{
	to = 0;
	memset(head, -1, sizeof(head));
}
void addedge(int x, int y, int z)
{
	edge[tot] = {y, z, head[x]};
	head[x] = tot++;
}
void solve()
{
	memset(vis, 0, sizeof(vis));
	memset(f, LL_INF, sizeof(f));
	f[1] = 0;
	queue <int> q;
	q.push(1);
	vis[1] = 1;
	while (!q.empty()) {
		int u = q.front(); q.pop();
		vis [u] = 0;
		for (int i = head[u]; i != -1; i = edge[i].nex)
        {
			int v = edge[i].to;
			if (f[u] + edge[i].w < f[v]) {
				f[v] = f[u] + edge[i].w;
				if (!vis[v]) {
					q.push(v);
					force[v] = 1;
				}
			}
		}
	}
}
intmain()
{
	while (~scanf("%d %d", &n, &m)) {
		init();
		for (int i = 1; i <= m; ++i) {
			int x, y, z;
			scanf("%d %d %d", &x, &y, &z);
			addedge(x, y, z);
			addedge(y, x, z);
		}
		solve();
		if (f[n] == LL_INF) puts("qwb baka");
		else printf("%lld\n", f[n]);
	}
	return 0;
}
To be continued. . .


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324419603&siteId=291194637