Dijkstra例题

【经典模板】:PID341 / 星门跳跃

题目大意:从1到N有M条边,每条边距离z,求最短路

思路:dijkstra。由于优先队列每次都是选取最小距离,则此距离固定,则代表已访问,标记点下次访问到此点直接跳过。
Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
const ll N=1010000;
int n;
const int inf=0x3f3f3f3f;
int dis[N];
bool vis[N];
vector<pair<int,int> >mp[N];
struct node
{
    int id;
    int len;
    friend bool operator <(node a,node b)
    {
        return a.len>b.len;
    }
};
void Dijkstra(int s)
{
    memset(dis,inf,sizeof(dis));
    memset(vis,false,sizeof(vis));
    dis[s]=0;
    priority_queue<node>Q;
    Q.push(node{s,0});
    while(!Q.empty())
    {
        node u=Q.top();
        Q.pop();
        int xx=u.id;
        if(vis[xx])
            continue;
        vis[xx]=true;
        for(int i=0;i<mp[xx].size();i++)
        {
            int to=mp[xx][i].first;
            if(!vis[to]&&dis[to]>dis[xx]+mp[xx][i].second)
            {
                dis[to]=dis[xx]+mp[xx][i].second;
                Q.push(node{to,dis[to]});
            }
        }
    }
}
int main()
{
    int m;
    scanf("%d %d",&n,&m);
    for(int i=0;i<m;i++)
    {
        int a,b,c;
        scanf("%d %d %d",&a,&b,&c);
        mp[a].push_back(make_pair(b,c));
        mp[b].push_back(make_pair(a,c));
    }
    Dijkstra(1);
    printf("%d\n",dis[n]);
    return 0;
}

726:ROADS
——路径有两个属性时的处理

题目大意:从1到N有m条边,每一条边有原点s,目的d,距离l和花费t,寻找从1到n的最短路,满足花费小于k

思路:dijkstra+priority_queue。由于从队列头取出的点不一定选取成功(即虽然路程最小但是花费>k),所以不能加vis和D数组的距离判断,只能把可能的路径放入队列,让优先队列自己判断
Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
const ll N=1010000;
int k,n,r;
const int inf=0x3f3f3f3f;
int dis[N],mon[N];
bool flag=false;

struct node
{
    int id;
    int len;
    int money;
    friend bool operator <(node a,node b)
    {
        if(a.len==b.len)
            return a.money>b.money;
        else
            return a.len>b.len;
    }
};
vector<node>mp[N];
void Dijkstra(int s)
{
    priority_queue<node>Q;
    node p;
    int id,len,money,v;
    memset(dis,-1,sizeof(dis));
    memset(mon,-1,sizeof(mon));
    dis[s]=0;
    mon[s]=0;
    Q.push(node{s,dis[s],mon[s]});
    while(!Q.empty())
    {
        p=Q.top();
        Q.pop();
        id=p.id;
        dis[id]=p.len;
        mon[id]=p.money;
        if(p.money>k)
            continue;
        if(id==n)
        {
            flag=true;
            return ;
        }
        for(int i=0; i<mp[id].size(); i++)
        {
            v=mp[id][i].id;
            len=mp[id][i].len;
            money=mp[id][i].money;
            Q.push(node{v,dis[id]+len,mon[id]+money});
        }
    }
};
int main()
{
    scanf("%d %d %d",&k,&n,&r);
    for(int i=0; i<r; i++)
    {
        int a,b,c,d;
        scanf("%d %d %d %d",&a,&b,&c,&d);
        mp[a].push_back(node{b,c,d});
    }
    Dijkstra(1);
    if(flag)
        printf("%d\n",dis[n]);
    else
        printf("-1\n");
    return 0;
}

1003 Emergency (25 分)
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.
Input Specification:
Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤500) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C ​1 ​​ and C ​2 ​​ - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c ​1 ​​ , c ​2 ​​ and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C ​1 ​​ to C ​2 ​​ .
Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C ​1 ​​ and C ​2 ​​ , and the maximum amount of rescue teams you can possibly gather. All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input:

5 6 0 2
1 2 1 5 3 
0 1 1
0 2 2 
0 3 1 
1 2 1 
2 4 1 
3 4 1 

Sample Output:

2 4

遍历结点,优先最短路,其次最大人数
Code:

#include <stdio.h>
#include <math.h>
#include<string.h>
#define INF 0x3f3f3f3f
int u[505]= {0};
int teams[505]= {0};
int dist[505];
int mp[505][505];
int n,m,st,en;
int shortNum=0,maxteam=0,mindist=INF;
void dfs(int s,int dis,int team)
{
    if(s==en)
    {
        if(dis<mindist)
        {
            mindist=dis;
            shortNum=1;
            maxteam=team;
        }
        else if(dis==mindist)
        {
            shortNum++;
            if(team>maxteam)
                maxteam=team;
        }
        return ;
    }
    u[s]=1;
    for(int i=0; i<n; i++)
    {
        if(u[i]==0&&mp[s][i]>0)
        {
            dfs(i,dis+mp[s][i],team+teams[i]);
        }
    }
    u[s]=0;
}

int main()
{
    scanf("%d %d %d %d",&n,&m,&st,&en);
    for(int i=0; i<n; i++)
        scanf("%d",&teams[i]);
    memset(mp,-1,sizeof(mp));
    for(int i=0; i<m; i++)
    {
        int t1,t2,dis;
        scanf("%d %d %d",&t1,&t2,&dis);
        mp[t1][t2]=mp[t2][t1]=dis;
    }
    dfs(st,0,teams[st]);
    printf("%d %d\n",shortNum,maxteam);
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=300;
const int INF=0x3f3f3f3f;
bool vis[maxn];
ll d[maxn];
int way[maxn][maxn];
int path[maxn];
int n,m,u,v,w;
bool once=true;
void Dijkstra()
{
    memset(vis,false,sizeof(vis));
    d[1]=0;
    for(int i=2;i<=n;i++)
        d[i]=INF;
    for(int i=1;i<=n;i++)
    {
        int x,m=INF;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&d[j]<=m)
            {
                m=d[j];
                x=j;
            }
        }
        vis[x]=true;
        for(int y=1;y<=n;y++)
        {
            if(way[x][y]==0)continue;
            if(d[y]>d[x]+way[x][y])
            {
                d[y]=d[x]+way[x][y];
                if(once)
                {
                    path[y]=x;
                }
            }
        }
    }
}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d %d %d",&u,&v,&w);
        way[u][v]=w;
        way[v][u]=w;
    }
    Dijkstra();
    ll minway=d[n];
    once=false;
    int pos=n;
    ll maxminway=minway;
    while(true)
    {
        way[pos][path[pos]]<<=1;
        way[path[pos]][pos]<<=1;
        Dijkstra();
        if(d[n]>maxminway)maxminway=d[n];
        way[pos][path[pos]]>>=1;
        way[path[pos]][pos]>>=1;
        if(pos==1)break;//反读
        else pos=path[pos];
    }
    printf("%lld\n",maxminway-minway);
    return 0;
}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据结构习题Dijkstra 1.3、将二叉树看作图,并对它作图的深度优先遍历,则与原二叉树的 结果是相同的。 A、前序遍历 B、中序遍历C、后序遍历D、层次序遍历 1.4、在关于树的以下4种叙述中,正确的是 。 A、用指针方式存储有n个结点的二叉树,至少要有n+1个指针。 B、m阶B-树中,具有k个子结点的结点,必有k-1个键值。 C、m阶B-树中,每个非叶子结点的子结点个数≥[m/2]。 D、平衡树一定是丰满树。 1.5、在最好和最坏情况下的时间复杂度均为O(nlog2n)且稳定的排序方法是 A、希尔排序 B、快速排序 C、堆排序 D、归并排序 二、解答题 2.1、对目标串 abaabcc和模式串aabc,描述根据KMP算法进行匹配的过程,包括失效函数计算。答:失效函数:-1, 0, -1, -1 目标串 abaabcc和模式串aabc的KMP算法进行匹配的过程 abaabcc aabc 首先,从目标位置0和模式位置0开始比较,至ab和aa,不等时目标位置为1, 模式位置为1。因0位置失效函数的值( f [posP-1] ) 为-1,从目标位 置1和模式位置0开始新一轮比较。因第一个字符( posP == 0 )就不等,目标 位置增1,目标位置2和模式位置0开始新一轮比较,直到模式位置比较至4, 匹配成功。 int fastFind ( char *s, char * pat ){ //带失效函数的KMP匹配算法 int posP = 0, posT = 0; int lengthP = strlen(pat), lengthT = strlen(s); while ( posP < lengthP && posT < lengthT ) if ( pat[posP] == s[posT] ) { posP++; posT++; //相等继续比较 } else if ( posP == 0 ) posT++; //不相等 else posP = f [posP-1]+1; if ( posP < lengthP ) return -1; else return posT - lengthP; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值