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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章