FJUT 3930 Shortest Path

Speaking with Guo moxin above to see a movie, so moxin and double Cheese bankrupt, desperation moxin we can only start a business selling vegetables

Of course, you have to purchase vegetables, so moxin went to the purchase, comparison shop here wonderful, observe the following rules:

1. When two different vegetables have the same value, it can be exchanged (for example, a value equal to the value b vegetables grow vegetables, vegetables can grow vegetables then a and b are interchanged, is not required to exchange spend the extra value)

2. can bargain in value + x value of RMB exchange for another kind of dish of vegetables

3. can be used directly equal to the value of RMB to purchase food kind of dish

So the question is, moxin want to know every purchase requires a minimum of vegetables, how much RMB?

Input
The first input line T (1 <= T <= 10), T group representatives

The second row input n, m (2 <= n <= 1e3, 0 <= m <= 1e3)

Representative vegetables relationship n, m kinds of bargaining

Next n lines, each line has two integers a, b (1 <= a <= n, 1 <= b <= 1e5)

N represents vegetables, growing vegetables on a value for b

Subsequently m rows, each row having three integers a, b, c (1 <= a <= n, 1 <= b <= n, 1 <= c <= 1e5, a! = B)

A representative of vegetables + c value of RMB exchange b can grow vegetables

(Data set 7 total)

Output
n行

Per line is an integer, the i represents an integer of desired vegetables each row corresponding to the minimum cost

SampleInput
1
3 1
1 7
2 2
3 2
1 3 1
SampleOutput
7
2
2

Meaning of the questions: can be obtained in three ways to make a certain dish worth less, seek to obtain minimum cost of all food
ideas: good thinking shortest path problem and then run dijstra build the relationship between each map vegetables get an answer, the more the difficulty lies in showing a specific construction of the code to see the comments
I did not look at the scope of violence Floyd hand rubbing my clothes and then presented a big TLE

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstdio>

using namespace std;
typedef pair<int,int> p;

const int maxn=1e3+5;
const int inf=0x3f3f3f3f;
int n,m;

struct edge
{
    int to;
    int cost;
    edge(int tt,int cc):to(tt),cost(cc){}
};

int dist[maxn];
int vis[maxn];

vector<edge>g[maxn];


void dj(int s)///di就不多说了 套板子就行
{
    fill(dist,dist+n+1,inf);
    priority_queue<p,vector<p>,greater<p> >que;

    dist[s]=0;
    que.push(p(0,s));

    while(!que.empty())
    {
        p now=que.top();
        que.pop();

        int temp=now.second;

        if(now.first>dist[temp])
            continue;

        for(int i=0; i<g[temp].size(); i++)
        {
            edge next=g[temp][i];

            if(dist[next.to]>dist[temp]+next.cost)
            {
                dist[next.to]=dist[temp]+(int)next.cost;

                que.push(p(dist[next.to],next.to));
            }
        }
    }
}


int main()
{
    int t;

    scanf("%d",&t);

    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(vis,0,sizeof(vis));///初始化

        for(int i=0; i<=n; ++i)
            g[i].clear();


        int a,b,c;

        for(int i=0; i<n; ++i)
        {
            scanf("%d%d",&a,&b);
            vis[a]=b;
            g[0].push_back(edge(a,b));///a种菜的价值为b
        }

        for(int i=0; i<m; ++i)
        {
            scanf("%d%d%d",&a,&b,&c);
            g[a].push_back(edge(b,c));///可以通过a种菜的花费c元得到b种菜
        }

        for(int i=1; i<=maxn; ++i)
        {
            for(int j=1; j<=maxn; ++j)
            {
                if(vis[i]!=0 && i!=j &&vis[i]==vis[j])///不同的菜有相同的价值是可以互换,那就在相同价值的a,b两种菜之间建立一条权值为0的边
                {
                    g[i].push_back(edge(j,0));
                    g[j].push_back(edge(i,0));
                }
            }
        }

        dj(0);

        for(int i=1; i<maxn; ++i)///输出即可
        {
            if(vis[i])
                printf("%d\n",dist[i]);
        }
    }
    return 0;
}
Published 54 original articles · won praise 0 · Views 1219

Guess you like

Origin blog.csdn.net/weixin_44144278/article/details/100025553