Guest User

Untitled

a guest
Jul 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.io.PrintWriter;
  6. import java.util.ArrayList;
  7. import java.util.StringTokenizer;
  8. import java.util.TreeSet;
  9.  
  10. public class chinese_sm_slow {
  11.  
  12. class Refrigerator {
  13.  
  14. ArrayList<Integer>[] eto, efrom;
  15. int[] u;
  16. int[] ans;
  17. int[] cond;
  18. int cnt;
  19.  
  20. void dfs(int v) {
  21. u[v] = 1;
  22. for (int i : efrom[v]) {
  23. if (u[i] == 0) {
  24. dfs(i);
  25. }
  26. }
  27. u[v] = 2;
  28. ans[--cnt] = v;
  29. }
  30.  
  31. void dfs2(int v, int c) {
  32. u[v] = 1;
  33. cond[v] = c;
  34. for (int i : eto[v]) {
  35. if (u[i] == 0) {
  36. dfs2(i, c);
  37. }
  38. }
  39. u[v] = 2;
  40. }
  41.  
  42. int n;
  43.  
  44. @SuppressWarnings("unchecked")
  45. public Refrigerator(int n) {
  46. this.n = n;
  47. eto = new ArrayList[n];
  48. efrom = new ArrayList[n];
  49. for (int i = 0; i < n; i++) {
  50. eto[i] = new ArrayList<Integer>(1);
  51. efrom[i] = new ArrayList<Integer>(1);
  52. }
  53. // for (int i = 0; i < m; i++) {
  54. // int x = nextInt() - 1;
  55. // int y = nextInt() - 1;
  56. // eto[x].add(y);
  57. // efrom[y].add(x);
  58. // }
  59. }
  60.  
  61. int[] solve() {
  62. u = new int[n];
  63. ans = new int[n];
  64. cnt = n;
  65. for (int i = 0; i < n; i++) {
  66. if (u[i] == 0) {
  67. dfs(i);
  68. }
  69. }
  70. u = new int[n];
  71. cond = new int[n];
  72. cnt = 1;
  73. for (int i = 0; i < ans.length; i++) {
  74. if (u[ans[i]] == 0) {
  75. dfs2(ans[i], cnt++);
  76. }
  77. }
  78. for (int i = 0; i < cond.length; i++) {
  79. cond[i] = cnt - cond[i] - 1;
  80. }
  81. // Arrays.fill(u, -1);
  82. // for (int i = 0; i < cond.length; i++) {
  83. // if (u[cond[i]] == -1) {
  84. // u[cond[i]] = i;
  85. // }
  86. // }
  87. // for (int i = 0; i < cond.length; i++) {
  88. // cond[i] = u[cond[i]];
  89. // }
  90. cnt--;
  91. return cond;
  92. }
  93. }
  94.  
  95. class Edge implements Comparable<Edge> {
  96. int from, to, c;
  97. int sourceC;
  98. Edge parent;
  99.  
  100. public Edge(int from, int to, int c) {
  101. this.c = c;
  102. this.from = from;
  103. this.to = to;
  104. sourceC = c;
  105. }
  106.  
  107. public Edge(int from, int to, Edge e) {
  108. this.from = from;
  109. this.to = to;
  110. this.c = e.c;
  111. this.sourceC = e.sourceC;
  112. this.parent = e;
  113. }
  114.  
  115. @Override
  116. public int compareTo(Edge o) {
  117. if (from < o.from)
  118. return -1;
  119. if (from > o.from)
  120. return 1;
  121. if (to < o.to)
  122. return -1;
  123. if (to > o.to)
  124. return 1;
  125. return 0;
  126. }
  127.  
  128. }
  129.  
  130. class Vertex {
  131. ArrayList<Edge> in, out;
  132. Edge zeroIn;
  133.  
  134. public Vertex() {
  135. in = new ArrayList<Edge>(1);
  136. out = new ArrayList<Edge>(1);
  137. }
  138. }
  139.  
  140. class Kever {
  141. ArrayList<Integer>[] e2;
  142.  
  143. @SuppressWarnings("unchecked")
  144. public Kever(int n) {
  145. e2 = new ArrayList[n];
  146. for (int i = 0; i < e2.length; i++) {
  147. e2[i] = new ArrayList<Integer>();
  148. }
  149. }
  150.  
  151. boolean[] u;
  152.  
  153. boolean isBald(int x) {
  154. u = new boolean[e2.length];
  155. dfs(x);
  156. for (int i = 0; i < u.length; i++) {
  157. if (!u[i]) {
  158. return false;
  159. }
  160. }
  161. return true;
  162. }
  163.  
  164. void dfs(int v) {
  165. u[v] = true;
  166. for (int x : e2[v]) {
  167. if (!u[x]) {
  168. dfs(x);
  169. }
  170. }
  171. }
  172. }
  173.  
  174.  
  175. class Graph {
  176. Vertex[] v;
  177. int n;
  178.  
  179. public Graph(int n) {
  180. v = new Vertex[n];
  181. this.n = n;
  182. all = new TreeSet<Edge>();
  183. select = new TreeSet<Edge>();
  184. for (int i = 0; i < v.length; i++) {
  185. v[i] = new Vertex();
  186. }
  187. }
  188.  
  189. TreeSet<Edge> all;
  190. TreeSet<Edge> select;
  191.  
  192. void addEdge(int from, int to, int c) {
  193. Edge e = new Edge(from, to, c);
  194. addEdge(e);
  195. }
  196.  
  197. void addEdge(Edge e) {
  198. if (e.from == e.to)
  199. return;
  200. if (all.contains(e)) {
  201. if (e.c < all.headSet(e, true).last().c) {
  202. all.remove(e);
  203.  
  204. all.add(e);
  205. }
  206. } else {
  207. all.add(e);
  208. }
  209.  
  210. }
  211.  
  212. void solve() {
  213. for (Edge e : all) {
  214. v[e.from].out.add(e);
  215. v[e.to].in.add(e);
  216. }
  217. Vertex start = v[0];
  218. for (Vertex x : v) {
  219. if (x == start) {
  220. continue;
  221. }
  222. int min = Integer.MAX_VALUE;
  223. for (Edge e : x.in) {
  224. min = Math.min(min, e.c);
  225. }
  226. for (Edge e : x.in) {
  227. e.c -= min;
  228. if (e.c == 0 && x.zeroIn == null) {
  229. // System.err.println(e.from + " " + e.to + " ("
  230. // + e.sourceC + ")");
  231. x.zeroIn = e;
  232. }
  233. }
  234. }
  235. Refrigerator cond = new Refrigerator(n);
  236. for (int i = 0; i < v.length; i++) {
  237. Vertex x = v[i];
  238. if (x.zeroIn != null) {
  239. cond.efrom[i].add(x.zeroIn.from);
  240. cond.eto[x.zeroIn.from].add(i);
  241. }
  242. }
  243. int[] a = cond.solve();
  244. // System.err.println(Arrays.toString(a));
  245. // System.err.println(cond.cnt);
  246. if (cond.cnt != n) {
  247. Graph g = new Graph(cond.cnt);
  248. for (Edge e : all) {
  249. g.addEdge(new Edge(a[e.from], a[e.to], e));
  250. }
  251. g.solve();
  252. for (Edge eNew : g.select) {
  253. Edge e = eNew.parent;
  254. v[e.to].zeroIn = e;
  255. }
  256. }
  257. for (int i = 0; i < v.length; i++) {
  258. Vertex x = v[i];
  259. if (x.zeroIn != null) {
  260. select.add(x.zeroIn);
  261. }
  262. }
  263.  
  264. }
  265. }
  266.  
  267.  
  268. private void solve() {
  269. int n = nextInt();
  270. int m = nextInt();
  271. Graph g = new Graph(n);
  272. for (int i = 0; i < m; i++) {
  273. g.addEdge(nextInt() - 1, nextInt() - 1, nextInt());
  274. }
  275. g.solve();
  276. Kever kever = new Kever(n);
  277. for (Edge e : g.select) {
  278. kever.e2[e.from].add(e.to);
  279. }
  280. if (!kever.isBald(0)) {
  281. out.println("NO");
  282. return;
  283. } else {
  284. out.println("YES");
  285. long ans = 0;
  286. for (Edge e : g.select) {
  287. ans += e.sourceC;
  288. }
  289. out.println(ans);
  290. }
  291.  
  292. }
  293.  
  294.  
  295. PrintWriter out;
  296. BufferedReader br;
  297. StringTokenizer st;
  298.  
  299. private void run() {
  300. try {
  301. br = new BufferedReader(new FileReader(this.getClass().getName()
  302. .substring(0, this.getClass().getName().indexOf("_"))
  303. + ".in"));
  304. out = new PrintWriter(this.getClass().getName().substring(0,
  305. this.getClass().getName().indexOf("_"))
  306. + ".out");
  307. solve();
  308. out.close();
  309. } catch (FileNotFoundException e) {
  310. e.printStackTrace();
  311. System.exit(1);
  312. }
  313. }
  314.  
  315. String nextToken() {
  316. try {
  317. while (st == null || !st.hasMoreTokens()) {
  318. st = new StringTokenizer(br.readLine());
  319. }
  320. return st.nextToken();
  321. } catch (IOException e) {
  322. return "";
  323. }
  324. }
  325.  
  326. int nextInt() {
  327. return Integer.parseInt(nextToken());
  328. }
  329.  
  330. public static void main(String[] args) {
  331. new chinese_sm_slow().run();
  332. }
  333. }
Add Comment
Please, Sign In to add comment