L. Magical Girl Haze 分层最短路、bfs + 优先队列+、最短路


L-Magical Girl Haze
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256MB
Problem Description
There are N cities in the country, and M directional roads from u to v(1<=u, v<=n).Every road has a distance ci. Haze is a Magical Girl that lives in City 1, she can choose no more than K roads and make their distances become 0. Now she wants to go to City N, please help her calculate the minimum distance.
Input
The first line has one integer T(1<=T<=5), then following T cases.
For each test case, the first line has three integers N, M and K. Then the following M lines each line has three integers, describe a road, Ui, Vi, Ci. There might be multiple edges between u and v.
It is guaranteed that N<=100000, M<=200000, K<=10, 0<=c[i]<=1e9. There is at least one path between City 1 and City N.
Output
For each test case, print the minimum distance.
Sample Input
1
5 6 1
1 2 2
1 3 4
2 4 3
3 4 1
3 5 6
4 5 2
Sample Output
3


此题的题意是:
有n个城市,m条有向路,现在可以忽略 k条路径,求最短路

代码


#include<iostream>
#include<vector>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;

struct node
{
    int t;
    ll cost;
};
int n,m,k;
int u,v,w;

vector<  vector<node> > edge(100005);
ll vis[100005][11];  //走到前k个城市  使用了 k次机会的最短路

struct pp
{
    int s;
    int times;
    ll cost;

    bool operator < (const pp & b)const{
         return cost > b.cost;
    }
};

void bfs()
{
    priority_queue<pp> qu;
    pp tmp;
    tmp.s = 1;
    tmp.cost =0;
    tmp.times=0;
    qu.push(tmp);
    vis[1][0] =0;


    pp f;
    while(!qu.empty())
    {
        f = qu.top();

        qu.pop();

        if(f.cost != vis[f.s][f.times])continue;


        for(int i=0;i<edge[f.s].size();i++)
        {

             if(f.cost + edge[f.s][i].cost <vis[edge[f.s][i].t][f.times])
             {
                 vis[edge[f.s][i].t][f.times] = f.cost + edge[f.s][i].cost;

                tmp.s = edge[f.s][i].t;
                 tmp.cost = f.cost + edge[f.s][i].cost;
                 tmp.times = f.times;
                 qu.push(tmp);
             }

             //如果该条边的权值不加上
             if(f.times <k&&f.cost < vis[edge[f.s][i].t][f.times+1])
             {
                 vis[edge[f.s][i].t][f.times+1] = f.cost;

                 tmp.cost = f.cost;
                 tmp.s = edge[f.s][i].t;
                 tmp.times = f.times+1;
                 qu.push(tmp);
             }
        }
    }
}
int main()
{
    int t;
    cin>>t;
    node tmp;

    for(int i=1;i<=t;i++)
    {
        cin>>n>>m>>k;
        for(int i=0;i<m;i++)
        {
             cin>>u>>tmp.t>>tmp.cost;
             edge[u].push_back(tmp);
        }

        memset(vis,inf,sizeof(vis));

        bfs();

        int mins = inf;
        for(int i=0;i<=k;i++)
        {
            if(vis[n][i] <mins)
            {
                mins = vis[n][i] ;
            }
        }

        cout<<mins<<endl;

        for(int i=0;i<=n;i++)edge[i].clear();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jackcily/article/details/82857725