Bzoj2324: [zjoi2011] Rescue of Pikachu

Source: Internet
Author: User
2324: [zjoi2011] Time Limit: 10 sec memory limit: 256 MB
Submit: 1359 solved: 522
[Submit] [Status] Description

Pikachu was snatched by the rockets with an evil strategy! These three bad guys have left Xiao Zhi with a provocation! For Pikachu and justice, Xiao Zhi and his friends are obliged to embark on the road to rescue Pikachu.

The rockets have a total of N positions and M two-way roads exist between positions. Data points are marked from 1 to n. Xiao Zhi and his team of K people started from Zhenxin town to rescue Pikachu, which was trapped in data point N. For convenience, we regard Zhenxin town as the data point No. 0. At the beginning, k people were all.

Due to the heavy protection of the rockets, to destroy the k point, must first in order to destroy 1 to the K-1 point, and if the K-1 point is not destroyed, due to the chain of defense, any person in Xiaozhi's line entering the data point K will be found and have serious consequences. Therefore, no one can pass through the data point K before the data point of the K-1 is destroyed.

To simplify the problem, we ignore the combat link. Any person in Xiaozhi's line passes through the data point K and thinks that the data point K is destroyed. The destroyed data point is still accessible.

K individual actions can be divided, as long as any person in the K-1 data point was destroyed, after K data point, K data point was destroyed. Apparently, as long as the data point N is destroyed, Pikachu will be saved.

The roads in the wild are insecure. Therefore, Xiaozhi and his entourage hope to save Pikachu by destroying data points n, so that the total length of the roads that K individuals travel will be the least.

Please help Xiaozhi design an optimal rescue solution!

 

Input

The first line contains three positive integers n, m, and K. Indicates a total of N + 1 data points, ranging from 0 to N, and m undirected edges. At the beginning, a total of K members of Xiaozhi's line were located.

In the next m row, there are three non-negative integers in each row. The integers in the I row are ai, Bi, and Li. Indicates that there is a road from the AI data point to the Bi data point whose length is Li.

Output

Contains only one INTEGER (s), which is the minimum total number of roads required for rescue of Pikachu.

Sample Input
3 4 2

0 1 1

1 2 1

2 3 100

0 3 1


Sample output3

[Example]

Xiao Zhi and Xiao Xia went together to rescue Pikachu. In the optimal solution, Xiao Zhi first goes from Zhenxin town to, and then to data point 2. After Xiaozhi successfully destroyed data point 2, Xiaoxia started from Zhenxin town and went directly to data point 3 to rescue Pikachu.

Hint

 



If 10% of the data is k = 1 and N = 3, Xiao Zhi will rescue Pikachu alone;


For 20% of the data, K ≤ 3 and N ≤ 20, the rockets, which were put out by Xiaozhi alone, strengthened their defense and increased their data points;


For 40% of the data, K ≤ 3 and N ≤ 100, in the face of enhanced defense, Xiao Zhi pulled a good friend Xiao Xia and Xiao Gang to rescue together;


For the other 20% of data, there are roads between any data points, and any 0 ≤ x, y, z ≤ n, with an inequality L (x, z) ≤ L (x, y) + L (Y, Z) was established;


For 100% of the data, the values of N ≤ 150, m ≤ 20 000, 1 ≤ k ≤ 10, and Li ≤ 10 000, ensure that a row of Xiao Zhi will be able to rescue Pikachu.


As for why K is less than or equal to 10, you can think that, at the call of Xiao Zhi, Xiao Xia, Xiao Gang, Xiao Jian, Xiao Yao, Xiao Sheng, Xiao Guang, Alice, Tian Tong, there is also a black cat chief who traveled to Japan to fight the rockets together.

 

Source

Day2

Question:

This is the same as the main idea of space racing and printer.

Each vertex must pass through and must pass through all vertices smaller than this vertex. There is also an additional source, but the source to the additional source has a certain capacity limit (interstellar does not ...)

We use the following method to create a diagram:

1. Split each vertex into two vertices: I and I + N, indicating that the vertex starts from and enters the vertex respectively.

2. edges with a connection capacity of 1 and a fee of 0 from S to all I

2. An edge with a capacity of 1 from all I + n to T and a cost of 0

3. from I to all J + N (j> N) capacity is 1, the cost is from I to J, does not pass through the shortest edge of the intermediate node greater than the J mark (otherwise this road will be invalid)

Correctness can be considered from the sources of I + N streams. Each stream method represents a real and valid solution. Cost is time-consuming and we need the shortest time, naturally, the maximum fee is required.

Another problem is that

The cost is from I to J. How can I solve this problem without going through the shortest circuit of the intermediate node greater than the J label?

I am using the following idea:

    for(int k=0;k<=n;k++)     for(int i=0;i<=n;i++)      for(int j=k;j<=n;j++)       f[i][j]=min(f[i][j],f[i][k]+f[k][j]);

This means that the node larger than J is not used as the intermediate node to update the shortest path from I (1. N) to J.

Therefore, after this process is completed, F [I] [J] indicates that the node from I to J does not pass through the Shortest Path greater than J.

Code:

 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #define inf 100000000012 #define maxn 50013 #define maxm 10000014 #define eps 1e-1015 #define ll long long16 #define pa pair<int,int>17 using namespace std;18 inline int read()19 {20     int x=0,f=1;char ch=getchar();21     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}22     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}23     return x*f;24 }25 int n,m,k,tot=1,s,t,ss,head[maxn],q[maxn],from[maxn],f[maxn][maxn];26 bool v[maxn];27 ll d[maxn],mincost;28 struct edge{int from,next,go,v,c;}e[2*maxm];29 void ins(int x,int y,int z,int w)30 {31     e[++tot].go=y;e[tot].from=x;e[tot].v=z;e[tot].c=w;e[tot].next=head[x];head[x]=tot;32 }33 void insert(int x,int y,int z,int w)34 {35     ins(x,y,z,w);ins(y,x,0,-w);36 }37 bool spfa()38 {39     for (int i=s;i<=ss;i++)d[i]=inf;40     memset(v,0,sizeof(v));41     int l=0,r=1,y;q[1]=s;d[s]=0;v[0]=1;42     while(l!=r)43     {44         int x=q[++l];if(l==maxn)l=0;v[x]=0;45         for (int i=head[x];i;i=e[i].next)46          if(e[i].v&&d[x]+e[i].c<d[y=e[i].go])47          {48             d[y]=d[x]+e[i].c;from[y]=i;49             if(!v[y]){v[y]=1;q[++r]=y;if(r==maxn)r=0;}50          }51     }52     return d[t]!=inf;53 }54 void mcf()55 {56     while(spfa())57     {58         int tmp=inf;59         for(int i=from[t];i;i=from[e[i].from]) tmp=min(tmp,e[i].v);60         mincost+=d[t]*tmp;61         for(int i=from[t];i;i=from[e[i].from]){e[i].v-=tmp;e[i^1].v+=tmp;}62     }63 }64 int main()65 {66     freopen("input.txt","r",stdin);67     freopen("output.txt","w",stdout);68     n=read();m=read();k=read();69     memset(f,60,sizeof(f));70     for(int i=1;i<=m;i++)71     {72         int x=read(),y=read(),z=read();73         f[x][y]=min(f[x][y],z);f[y][x]=min(f[y][x],z);74     }75     s=0;t=2*n+1;ss=2*n+2;insert(s,ss,k,0);76     for(int k=0;k<=n;k++)77      for(int i=0;i<=n;i++)78       for(int j=k;j<=n;j++)79        f[i][j]=min(f[i][j],f[i][k]+f[k][j]);80     for(int i=1;i<=n;i++)insert(s,i,1,0);81     for(int i=1;i<=n;i++)insert(ss,i+n,1,f[0][i]);82     for(int i=1;i<=n;i++)insert(i+n,t,1,0);83     for(int i=1;i<=n;i++)84      for(int j=i+1;j<=n;j++)85       insert(i,j+n,1,f[i][j]);86     mcf();87     printf("%lld\n",mincost);     88     return 0;89 }
View code

 

 

Bzoj2324: [zjoi2011] Rescue of Pikachu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.