--------------------------------- heat wave graph theory (SPFA) / Shortest

Texas simplicity of the people who are suffering from this summer's heat wave huge! ! !

Their Texas Longhorns tastes good, but they are not very good at producing cream enriched dairy products.

Farmer John took the lead this time to take up a large amount of cold milk nutrient delivery to the task of Texas, in order to reduce heat Texans endure the pain.

John has studied possible to transport milk from Wisconsin to Texas route.

The route includes a total of start and end points T towns, in order to facilitate reference numeral 1 T.

Each town in addition to the start and end of the two-way by road to connect at least two other towns.

Each has a through road costs (including fuel costs, tolls, etc.).

Given a map with a direct connection to the road C Article 2 towns.

Each path from the starting point of the road Rs, Re and spent Ci end components.

Te town seeking the minimum total cost from the town to the end of the initial Ts.

Input format
First line: 4 integers separated by spaces: T, C, Ts, Te ;

2 through C + 1: Line i + 1 i-th row is described roads, comprising three integers separated by spaces: Rs, Re, Ci.

Output format of
a single integer from the minimum total cost of Ts to Te.

Data guarantee the existence of at least one road.

Data range
1≤T≤2500,
1≤C≤6200,
1≤Ts, Te, Rs of, Re≤T,
1≤Ci≤1000
input sample:
. 7. 5. 4. 11
2 2. 4
. 1. 4. 3
. 7 2 2
. 3. 4 . 3
. 5 7. 5
7. 3. 3
. 6. 1. 1
. 6. 3. 4
2. 4. 3
. 5. 6. 3
7 2. 1
output sample:
7

Analysis:
Shortest board


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+1000;
int t,c,sx,ex;
int h[N],e[N],ne[N],idx,w[N];
int dist[N];
bool st[N];
void add(int a,int b,int c)
{
    e[idx]=b;w[idx]=c;ne[idx]=h[a];h[a]=idx++;
}
void spfa()
{
    memset(dist,0x3f,sizeof dist);
    dist[sx]=0;
    queue<int> q;
    q.push(sx);
    st[sx]=true;
    while(q.size())
    {
        int t=q.front();
        q.pop();
        st[t]=false;
        for(int i=h[t];~i;i=ne[i])
        {
            int j=e[i];
            if(dist[j]>dist[t]+w[i])
            {
                dist[j]=dist[t]+w[i];
                if(!st[j])
                {
                     q.push(j);
                    st[j]=true;
                }
            }
        }
    }
    
}
int main()
{
    cin>>t>>c>>sx>>ex;
    memset(h,-1,sizeof h);
    for(int i=0;i<c;i++)
    {
        int a,b,w;
        cin>>a>>b>>w;
        add(a,b,w);
        add(b,a,w);
    }
    spfa();
    cout<<dist[ex]<<endl;
}
Published 383 original articles · won praise 7 · views 8024

Guess you like

Origin blog.csdn.net/qq_43690454/article/details/104276931