标签:
ID
|
Origin
|
Title
| ||
---|---|---|---|---|
59 / 211 | Problem A | UVALive 6318 | The New President | |
71 / 156 | Problem B | UVALive 6319 | No Name | |
38 / 127 | Problem C | UVALive 6320 | Encrypted Password | |
87 / 127 | Problem D | UVALive 6321 | Kids Love Candies | |
1 / 20 | Problem E | UVALive 6322 | The Swapping Game | |
0 / 2 | Problem F | UVALive 6323 | Lock Pattern | |
23 / 94 | Problem G | UVALive 6324 | Archery | |
Problem H | UVALive 6325 | Connecting Islands | ||
52 / 121 | Problem I | UVALive 6326 | Contest Hall Preparation | |
1 / 1 | Problem J | UVALive 6327 | Math Homework |
1 #include <cstdio> 2 #include <vector> 3 #include <cstring> 4 #include <queue> 5 6 using namespace std; 7 const int maxv = 1e+4; 8 const int maxe = 1e+6;; 9 const long long INF = 0x3f3f3f3f; 10 11 struct Edge { 12 int from, to, cap, flow, cost; 13 Edge() {} 14 Edge(int from, int to, int cap, int flow, int cost) { 15 this->from = from; 16 this->to = to; 17 this->cap = cap; 18 this->flow = flow; 19 this->cost = cost; 20 } 21 }; 22 struct MCMF { 23 int n, m, s, t; 24 vector<Edge> edges; 25 vector<int> G[maxv]; 26 int inq[maxe]; 27 int d[maxe]; 28 int p[maxe]; 29 int a[maxe]; 30 31 void init(int n) { 32 this -> n = n; 33 for (int i = 0; i < n; i++) G[i].clear(); 34 edges.clear(); 35 } 36 void AddEdge(int from, int to, int cap, int cost) { 37 edges.push_back(Edge(from, to, cap, 0, cost)); 38 edges.push_back(Edge(to, from, 0, 0, -cost)); 39 m = edges.size(); 40 G[from].push_back(m-2); 41 G[to].push_back(m-1); 42 } 43 44 bool BellmanFord(int s, int t, int &flow, int &cost) { 45 for (int i = 0; i < n; i++) d[i] = INF; 46 memset(inq, 0, sizeof(inq)); 47 d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF; 48 49 queue<int>Q; 50 Q.push(s); 51 while (!Q.empty()) { 52 int u = Q.front(); Q.pop(); 53 inq[u] = 0; 54 for (int i = 0; i < (int)G[u].size(); i++) { 55 Edge &e = edges[G[u][i]]; 56 if (e.cap > e.flow && d[e.to] > d[u] + e.cost) { 57 d[e.to] = d[u] + e.cost; 58 p[e.to] = G[u][i]; 59 a[e.to] = min(a[u], e.cap - e.flow); 60 if (!inq[e.to]) {Q.push(e.to); inq[e.to] = 1;} 61 } 62 } 63 } 64 if (d[t] == INF) return false; 65 flow += a[t]; 66 cost += d[t] * a[t]; 67 int u = t; 68 while (u != s) { 69 edges[p[u]].flow += a[t]; 70 edges[p[u]^1].flow -= a[t]; 71 u = edges[p[u]].from; 72 } 73 return true; 74 } 75 76 int Mincost (int s, int t) { 77 int flow = 0, cost = 0; 78 while (BellmanFord(s, t, flow, cost)); 79 return flow; 80 } 81 }G; 82 83 char S[1000], tep[10]; 84 int vis[26+10], num[26+10]; 85 86 int main() { 87 int t; 88 freopen("in.txt", "r", stdin); 89 scanf("%d", &t); 90 while (t--) { 91 scanf("%s", S); 92 int lenS = strlen(S); 93 int s = lenS+26, t = lenS + 27; 94 G.init(t+2); 95 memset(num, 0, sizeof(num)); 96 for (int i = 0; i < lenS; i++) { 97 num[S[i] - ‘a‘]++; 98 G.AddEdge(s, i, 1, 0); 99 } 100 101 for (int i = 0 ; i < lenS; i++) { 102 scanf("%s", tep); 103 int lentep = strlen(tep); 104 memset(vis, 0, sizeof(vis)); 105 for (int j = 0; j < lentep; j++) { 106 vis[tep[j] - ‘a‘]++; 107 } 108 for (int j = 0; j < 26; j++) { 109 if (vis[j]) { 110 G.AddEdge(i, lenS+j, vis[j], 10*i + 1000*j); 111 } 112 } 113 } 114 for (int i = 0; i < 26; i++) { 115 G.AddEdge(lenS+i, t, num[i], 0); 116 } 117 if (G.Mincost(s, t) < lenS) { 118 puts("NO SOLUTION"); 119 } else { 120 for (int i = 0; i < (int)G.edges.size(); i += 2) { 121 int u = G.edges[i].from; 122 int v = G.edges[i].to; 123 int flow = G.edges[i].flow; 124 int cost = G.edges[i].cost; 125 int cap = G.edges[i].cap; 126 //printf("%d %d (%d %d) %d\n", u, v, flow, cap, cost); 127 if (lenS <= v && v < lenS + 26 && flow > 0) 128 printf("%c", ‘a‘ + v - lenS); 129 } 130 puts(""); 131 } 132 } 133 return 0; 134 }
标签:
原文地址:http://www.cnblogs.com/cheater/p/4693023.html