首页 > 其他 > 详细

cf div2 234 D

时间:2014-03-07 05:25:22      阅读:453      评论:0      收藏:0      [点我收藏+]
D. Dima and Bacteria
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Dima took up the biology of bacteria, as a result of his experiments, he invented k types of bacteria. Overall, there are n bacteria at his laboratory right now, and the number of bacteria of type i equals ci. For convenience, we will assume that all the bacteria are numbered from 1 to n. The bacteria of type ci are numbered from bubuko.com,布布扣 to bubuko.com,布布扣.

With the help of special equipment Dima can move energy from some bacteria into some other one. Of course, the use of such equipment is not free. Dima knows m ways to move energy from some bacteria to another one. The way with number i can be described with integers ui, vi and xi mean that this way allows moving energy from bacteria with number ui to bacteria with number vi or vice versa for xi dollars.

Dima‘s Chef (Inna) calls the type-distribution correct if there is a way (may be non-direct) to move energy from any bacteria of the particular type to any other bacteria of the same type (between any two bacteria of the same type) for zero cost.

As for correct type-distribution the cost of moving the energy depends only on the types of bacteria help Inna to determine is the type-distribution correct? If it is, print the matrix d with size k?×?k. Cell d[i][j] of this matrix must be equal to the minimal possible cost of energy-moving from bacteria with type i to bacteria with type j.

Input

The first line contains three integers n,?m,?k (1?≤?n?≤?105; 0?≤?m?≤?105; 1?≤?k?≤?500). The next line contains k integers c1,?c2,?...,?ck (1?≤?ci?≤?n). Each of the next m lines contains three integers ui,?vi,?xi (1?≤?ui,?vi?≤?105; 0?≤?xi?≤?104). It is guaranteed that bubuko.com,布布扣.

Output

If Dima‘s type-distribution is correct, print string ?Yes?, and then k lines: in the i-th line print integers d[i][1],?d[i][2],?...,?d[i][k] (d[i][i]?=?0). If there is no way to move energy from bacteria i to bacteria j appropriate d[i][j] must equal to -1. If the type-distribution isn‘t correct print ?No?.

Sample test(s)
Input
4 4 2
1 3
2 3 0
3 4 0
2 4 1
2 1 2
Output
Yes
0 2
2 0
Input
3 1 2
2 1
1 2 0
Output
Yes
0 -1
-1 0
Input
3 2 2
2 1
1 2 0
2 3 1
Output
Yes
0 1
1 0
Input
3 0 2
1 2
Output
No

好吧,cf第四道居然这么水,虽然死长死长的,每个类型中取一个点深搜只走权为0的路,然后判断能不能覆盖同类型的所有点即可判断,然后更新dis[i][j] ( 1<=i<= k, 1<=j<=k)的值,走一次floyd即可
bubuko.com,布布扣
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 
  6 using namespace std;
  7 
  8 #define maxn 100005
  9 
 10 int n,m,k;
 11 int c[505],first[maxn],next[2 * maxn],v[2 * maxn],x[2 * maxn],dis[505][505],u1[2 * maxn];
 12 bool flag = 1;
 13 bool vis[maxn];
 14 
 15 void addedge(int a,int b,int id) {
 16         int e = first[a];
 17         next[id] = e;
 18         first[a] = id;
 19 }
 20 
 21 void dfs(int u,int type) {
 22         vis[u] = 1;
 23         //printf("u = %d type = %d\n",u,type);
 24         for(int e = first[u]; e != -1; e = next[e]) {
 25                 if(!vis[ v[e] ] && x[e] == 0) {
 26                         dfs(v[e],type);
 27                 }
 28         }
 29 }
 30 void solve() {
 31         for(int i = 1; i <= k; i++) {
 32                 memset(vis,0,sizeof(vis));
 33                 dfs(c[i],i);
 34 
 35                 for(int j = c[i - 1] + 1; j <= c[i]; j++) {
 36                         if(!vis[j]) {
 37                             flag = 0;
 38                             //printf(" i = %d\n",i);
 39                             return;
 40                         }
 41                 }
 42 
 43         }
 44 }
 45 
 46 void floyd() {
 47         for(int p = 1; p <= k; p++) {
 48                 for(int i = 1; i <= k; i++) {
 49                         for(int j = 1; j <= k; j++) {
 50                                 if(dis[i][p] != -1 && dis[p][j] != -1) {
 51                                          dis[i][j] = dis[i][j] == -1 ? dis[i][p] + dis[p][j] :
 52                                                     min(dis[i][j],dis[i][p] + dis[p][j]);
 53                                 }
 54                         }
 55                 }
 56         }
 57 }
 58 void output() {
 59         if(flag) {
 60                 printf("Yes\n");
 61 
 62                 for(int i = 0; i < 2 * m; i += 2 ) {
 63                         int id1,id2;
 64                         id1 = lower_bound(c + 1,c + k + 1,u1[i]) - c;
 65                         id2 = lower_bound(c + 1,c + k + 1,v[i]) - c;
 66                         if(id1 == id2) continue;
 67                         dis[id1][id2] = dis[id2][id1] = dis[id1][id2] == -1 ?
 68                                         x[i] : min(dis[id1][id2],x[i]);
 69                 }
 70 
 71                 floyd();
 72 
 73                 for(int i = 1; i <= k; i++) {
 74                         for(int j = 1; j <= k; j++) {
 75                                 printf("%d",dis[i][j]);
 76                                 if(j != k) printf(" ");
 77                         }
 78                         printf("\n");
 79                 }
 80         } else {
 81                 printf("No\n");
 82         }
 83 }
 84 
 85 
 86 int main() {
 87 
 88         freopen("sw.in","r",stdin);
 89 
 90         scanf("%d%d%d",&n,&m,&k);
 91 
 92         for(int i = 1; i <= k; i++) {
 93                 scanf("%d",&c[i]);
 94         }
 95 
 96         for(int i = 1; i <= k; i++) {
 97                 c[i] += c[i - 1];
 98         }
 99 
100         for(int i = 1; i <= k; i++) {
101                 for(int j = 1; j <= k; j++) {
102                         if(i == j) dis[i][j] = 0;
103                         else dis[i][j] = -1;
104                 }
105         }
106 
107 
108         for(int i = 1; i <= n; i++) first[i] = -1;
109 
110         for(int i = 0; i < 2 * m; i = i + 2) {
111                 int a,b,w;
112                 scanf("%d%d%d",&u1[i],&v[i],&x[i]);
113                 v[i + 1] = u1[i];
114                 u1[i + 1] = v[i];
115                 x[i + 1] = x[i];
116                 addedge(u1[i],v[i],i);
117                 addedge(v[i],u1[i],i + 1);
118         }
119 
120         solve();
121 
122         output();
123 
124         return 0;
125 
126 
127 }
View Code

cf div2 234 D,布布扣,bubuko.com

cf div2 234 D

原文:http://www.cnblogs.com/hyxsolitude/p/3585235.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!