【fzu-2261】A wave in a wave

TonyY is a man who likes to roam everywhere. His dream is to take Sister Lan Lan to every corner of the heaven, but before that, he needs to make a plan.

Now he has a map of the Celestial Dynasty in his hand with n cities and m traffic paths, each of which is a one-way street. He has pre-planned some points as the starting point and ending point of the tour, and he wants to choose one of the starting points and one ending point, and find a route from the starting point to the ending point to experience the process of the waves firsthand. But he has limited time, so he wants to choose the least time-consuming one. Can you tell him how much is the least time-consuming?

Input

 

Contains multiple sets of test data.

The first line of input includes two integers n and m, indicating that there are n locations and m feasible paths. Points are numbered 1 - n.

Each of the next m lines includes three integers i, j, and cost, indicating the time-consuming cost from location i to location j.

The first number in the next line is S, indicating the number of possible starting points, and the number of S after that, indicating the possible starting points.

The first number in the next line is E, indicating the number of possible ending points, and the number of E after that, indicating the possible ending point.

0<S, E≤n≤100000,0<m≤100000,0<cost≤100。

Output

Output the shortest time he needs.

Sample Input

4 4
1 3 1
1 4 2
2 3 3
2 4 4
2 1 2
2 3 4

Sample Output

1

Build a fried chicken source point and fried chicken sink, and run SPFA again

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int N = 1e5+4;
vector<pair<int, int> >V[N];
int dis[N], n;
bool vis[N];
void SPFA()
{
    for(int i = 0; i <= n+1; i++)
    {
        dis[i] = 1e9;
        show [i] = 0 ;
    }
    dis[ 0 ] = 0 , vis[ 0 ] = 1 ;
    queue<int>Q;
    Q.push(0);
    while(!Q.empty())
    {
        int s = Q.front();
        Q.pop();
        vis[s] = 0;
        for(unsigned int i = 0; i < V[s].size(); i++)
        {
            int v = V[s][i].first, w = V[s][i].second;
            if(dis[v] > dis[s] + w)
            {
                dis[v] = dis[s] + w;
                if(!vis[v])
                {
                    force[v] = 1 ;
                    Q.push(v);
                }
            }
        }
    }
}
intmain ()
{
    int m, a, b, c;
    while(~scanf("%d%d", &n, &m))
    {
        for(int i = 0; i <= n+1; i++) V[i].clear();
        while(m--)
        {
            scanf("%d%d%d", &a, &b, &c);
            V[a].push_back(make_pair(b, c));
        }
        cin>>m;
        while(m--)
        {
            scanf("%d", &a);
            V[0].push_back(make_pair(a, 0));
        }
        cin>>m;
        while(m--)
        {
            scanf("%d", &a);
            V[a].push_back(make_pair(n+1, 0));
        }
        SPFA();
        printf("%d\n", dis[n+1]);
    }

    return 0;
}

Guess you like

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