Luo Gu P4822 [BJWC2012] freeze solution to a problem

P4822 [BJWC2012] Freeze

Title Description

"I want to become a magical girl!"
"Well, at the expense of the soul, what do you want?"
"I want everything about magic and miracles, among the seal on the card." ""

After this wish is realized world, people enjoy the magic card (SpellCard, also known as Spell Card) brings convenient.

Now, do not need to set a contract can also use magic! You do not to try?
For example, we "freeze" as a keyword query, there will be many interesting results used in magic Encyclopedia (Encyclopedia of Spells) years.
For example, we know Cirno, her frozen magic, of course there will be a corresponding SpellCard. Of course, even more surprisingly, there are as many magic to freeze time, Cirno frozen frog is really trivial compared to those coming up.
This shows that the world before, there are many magical girl had a wish to control the time, such as Akemi Homura, Sakuya Izayoi, ""
Of course, in this question, we are not coming to the study of history, but research applications of magic.

We consider the simplest trip all right: now there are N cities, the M two-way street on this continent. Cities are numbered 1 ~ N, we are No. 1 in the city, the city needs to N numbers, how can the fastest to reach it?
Is not that the shortest path problem? We all know that you can use Dijkstra, Bellman-Ford, Floyd- Warshall algorithms to solve.
Now, we have a total K Zhang can make time to slow down to 50% of SpellCard, that is to say, when passing through a path, we can choose to use a card, so that by the time we have this road could be reduced to the original of the half. have to be aware of is:

  1. Only use up a SpellCard on the road.
  2. Use a SpellCard works only on a road.
  3. You do not have to use all finished SpellCard.

Given the above information, your task is to: find in you can use it no more than the case of K SpellCard Zhang deceleration time, from city to city 1 N least how long it takes.

Input Format

The first line contains three integers: N, M, K.
Next M rows, each row comprising three integers: Ai, Bi, Timei, indicating the presence of a two-way road between Ai and Bi, without the use of SpellCard, by the time it takes to Timei.

Output Format

An output integer N represents the minimum number of cities to the city with the number 1.

Sample input and output

Input # 1

4 4 1
1 2 4
4 2 6
1 3 8
3 4 8

Output # 1

7

Description / Tips

Sample Explanation:
When not in use SpellCard, for the shortest 1à2à4, a total time of 10. Now we can use once SpellCard, then we will be halved by the time 2à4 this road, this time a total time of 7.
To 100% of the data: 1 ≤ K ≤ N ≤ 50 , M ≤ 1000.
1≤ Ai, Bi ≤ N, 2 ≤ Timei ≤ 2000.
To ensure that the answer is an integer, ensure that all Timei are even.
All data in the non-guaranteed to FIG Loop-free, multiple edges, and is connected.

[Thinking]

Figure + dijkstra stratified
hierarchical view of the board title
if you want to know the hierarchical diagram see here
understand the layered graph

[Title] effect

From 1 to n run
which allows k road becomes time consuming half the original
demand minimum time-consuming

[Analysis] title

If you are not the first to make the most of a short circuit, then
k see this article halving road
can not help but think of the strip k free road
so expect something simple hierarchical view of the
essentially free of the K Road and naughty minus k half-way is a kind of treatment
can be done in a hierarchical FIG.

[Core] ideas

The topic given the assignment of drawing k times
and then the path between the two figures are marked by half the distance
because the road is used magic
and then you can run naked dijkstra a
comparison with zero-order magic to k times inside magic reach the end of
time which consumes the least is the answer

【note】

Do hierarchical diagram when the subject
must be on the spatial extent strictly
or else it is easy to go wrong
Tips: If you do not know how open, how big it is able to open to open much

[Complete code]

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring> 
using namespace std;
struct point
{
    int w,x;
    bool operator < (const point & xx)const 
    {
        return xx.w < w;
    }
};
const int Max = 100005;
struct node
{
    int y,ne,z;
}a[20 * Max];
int sum = 0;int head[Max];

void add(int x,int y,int z)
{
    a[++ sum].y = y;
    a[sum].ne = head[x];
    a[sum].z = z;
    head[x] = sum;
}
int d[Max];
bool use[Max];
priority_queue<point>q;
void dj()
{
    memset(d,0x3f,sizeof(d));
    d[1] = 0;
    q.push((point){0,1});
    while(!q.empty())
    {
        point qwq = q.top();
        q.pop();
        int x = qwq.x,w = qwq.w;
        if(use[x] == true)
            continue;
        else
            use[x] = true;
        for(register int i = head[x];i != 0;i = a[i].ne)
        {
            int awa = a[i].y;
            if(d[awa] > d[x] + a[i].z)
            {
                d[awa] = d[x] + a[i].z;
                if(use[awa] == false)
                    q.push((point){d[awa],awa});
            }
        }
    }
}

int main()
{
    int n,m,k;
    cin >> n >> m >> k;
    int x,y,z;
    for(register int i = 1;i <= m;++ i)
    {
        cin >> x >> y >> z;
        add(x,y,z);
        add(y,x,z);
        for(register int j = 1;j <= k;++ j)
        {
            add(j * n + x,j * n + y,z);
            add(j * n + y,j * n + x,z);
            add((j - 1) * n + x,j * n + y,z / 2);
            add((j - 1) * n + y,j * n + x,z / 2);
        }
    }
    dj();
    int M = 0x7fffffff;
    for(register int i = 0;i <= k;++ i)
        M = min(M,d[i * n + n]);
    cout << M << endl;
    return 0;
}

I want to become a magical girl QWQ

Guess you like

Origin www.cnblogs.com/acioi/p/11724017.html