bzoj 2324 营救皮卡丘

2324: [ZJOI2011]营救皮卡丘

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 1440  Solved: 560

 

Description

皮卡丘被火箭队用邪恶的计谋抢走了!这三个坏家伙还给小智留下了赤果果的挑衅!为了皮卡丘,也为了正义,小智和他的朋友们义不容辞的踏上了营救皮卡丘的道路。

火箭队一共有N个据点,据点之间存在M条双向道路。据点分别从1N标号。小智一行K人从真新镇出发,营救被困在N号据点的皮卡丘。为了方便起见,我们将真新镇视为0号据点,一开始K个人都在0号点。

由于火箭队的重重布防,要想摧毁K号据点,必须按照顺序先摧毁1K-1号据点,并且,如果K-1号据点没有被摧毁,由于防御的连锁性,小智一行任何一个人进入据点K,都会被发现,并产生严重后果。因此,在K-1号据点被摧毁之前,任何人是不能够经过K号据点的。

为了简化问题,我们忽略战斗环节,小智一行任何一个人经过K号据点即认为K号据点被摧毁。被摧毁的据点依然是可以被经过的。

K个人是可以分头行动的,只要有任何一个人在K-1号据点被摧毁之后,经过K号据点,K号据点就被摧毁了。显然的,只要N号据点被摧毁,皮卡丘就得救了。

野外的道路是不安全的,因此小智一行希望在摧毁N号据点救出皮卡丘的同时,使得K个人所经过的道路的长度总和最少。

请你帮助小智设计一个最佳的营救方案吧!

 

Input

第一行包含三个正整数N,M,K。表示一共有N+1个据点,分别从0N编号,以及M条无向边。一开始小智一行共K个人均位于0号点。 

接下来M行,每行三个非负整数,第i行的整数为Ai,Bi,Li。表示存在一条从Ai号据点到Bi号据点的长度为Li的道路。

Output

仅包含一个整数S,为营救皮卡丘所需要经过的最小的道路总和。

Sample Input


3 4 2

0 1 1

1 2 1

2 3 100

0 3 1


Sample Output

3

【样例说明】

小智和小霞一起前去营救皮卡丘。在最优方案中,小智先从真新镇前往1号点,接着前往2号据点。当小智成功摧毁2号据点之后,小霞从真新镇出发直接前往3号据点,救出皮卡丘。

HINT

 



对于10%的数据满足 K = 1,且N = 3,小智将独自前去营救皮卡丘;


对于20%的数据满足 K ≤ 3,且N ≤ 20,被小智单挑剿灭的火箭队加强了防御,增加了据点数;


对于40%的数据满足 K ≤ 3,且N ≤ 100,面对加强的防御,小智拉来了好朋友小霞和小刚,一同前去营救;


对于另外20%的数据满足任意一对据点之间均存在道路,并且对任意的0 ≤ X,Y,Z ≤ N,有不等式L(X,Z) ≤ L(X,Y) + L(Y,Z)成立;


对于100%的数据满足N ≤ 150, M ≤ 20 000, 1 ≤ K ≤ 10, Li ≤ 10 000, 保证小智一行一定能够救出皮卡丘。


至于为什么K ≤ 10,你可以认为最终在小智的号召下,小智,小霞,小刚,小建,小遥,小胜,小光,艾莉丝,天桐,还有去日本旅游的黑猫警长,一同前去大战火箭队。

 

Source

Day2

解题:最近复习数据库复习傻逼了,这题写到吐血,终于AC了。。。

  1 #include <iostream>

  2 #include <cstdio>

  3 #include <cstring>

  4 #include <cmath>

  5 #include <algorithm>

  6 #include <climits>

  7 #include <vector>

  8 #include <queue>

  9 #include <cstdlib>

 10 #include <string>

 11 #include <set>

 12 #include <stack>

 13 #define LL long long

 14 #define pii pair<int,int>

 15 #define INF 0x3f3f3f3f3f3f3fLL

 16 using namespace std;

 17 const int maxn = 600;

 18 struct arc {

 19     int to,next;

 20     LL flow,cost;

 21     arc(int x = 0,LL y = 0,LL z = 0,int nxt = -1) {

 22         to = x;

 23         flow = y;

 24         cost = z;

 25         next = nxt;

 26     }

 27 };

 28 arc e[maxn*maxn];

 29 int head[maxn],p[maxn];

 30 LL d[maxn],dis[maxn][maxn];

 31 int tot,S,T,N,M,K;

 32 bool in[maxn];

 33 void add(int u,int v,LL flow,LL cost) {

 34     e[tot] = arc(v,flow,cost,head[u]);

 35     head[u] = tot++;

 36     e[tot] = arc(u,0,-cost,head[v]);

 37     head[v] = tot++;

 38 }

 39 bool spfa() {

 40     for(int i = 0; i < maxn; ++i) {

 41         d[i] = INF;

 42         p[i] = -1;

 43         in[i] = false;

 44     }

 45     queue<int>q;

 46     d[S] = 0;

 47     q.push(S);

 48     while(!q.empty()) {

 49         int u = q.front();

 50         q.pop();

 51         in[u] = false;

 52         for(int i = head[u]; ~i; i = e[i].next) {

 53             if(e[i].flow && d[e[i].to] > d[u] + e[i].cost) {

 54                 d[e[i].to] = d[u] + e[i].cost;

 55                 p[e[i].to] = i;

 56                 if(!in[e[i].to]) {

 57                     in[e[i].to] = true;

 58                     q.push(e[i].to);

 59                 }

 60             }

 61         }

 62     }

 63     return p[T] > -1;

 64 }

 65 LL solve() {

 66     LL ans = 0;

 67     while(spfa()) {

 68         for(int i = p[T]; ~i; i = p[e[i^1].to]) {

 69             --e[i].flow;

 70             ++e[i^1].flow;

 71         }

 72         ans += d[T];

 73     }

 74     return ans;

 75 }

 76 void Floyd() {

 77     for(int k = 0; k <= N; ++k)

 78         for(int i = 0; i <= N; ++i)

 79             for(int j = 0; j <= N; ++j)

 80                 if(k <= i || k <= j)

 81                     dis[i][j] = min(dis[i][j],dis[i][k]+dis[k][j]);

 82 }

 83 int main() {

 84     int u,v;

 85     LL w;

 86     while(~scanf("%d %d %d",&N,&M,&K)) {

 87         memset(head,-1,sizeof(head));

 88         for(int i = tot = 0; i <= N; ++i)

 89             for(int j = 0; j <= N; ++j)

 90                 dis[i][j] = i == j?0:INF;

 91         for(int i = tot = 0; i < M; ++i) {

 92             cin>>u>>v>>w;

 93             dis[u][v] = dis[v][u] = min(dis[u][v],w);

 94         }

 95         Floyd();

 96         S = (N+1)<<1;

 97         T = S|1;

 98         for(int i = 0; i <= N; ++i)

 99             add(i<<1,i<<1|1,1LL,-INF);

100         for(int i = 0; i <= N; ++i){

101             add(i<<1|1,T,1,0);

102             for(int j = i + 1; j <= N; ++j)

103                 add(i<<1|1,j<<1,1,dis[i][j]);

104         }

105         add(S,1,K,0);

106         cout<<N*INF+solve()<<endl;;

107     }

108     return 0;

109 }
View Code

 

你可能感兴趣的:(ZOJ)