革命之路漫漫

第一次尝试 40points spfa

  1. #include <bits/stdc++.h>
  2. #define read read()
  3. using namespace std;
  4.  
  5. inline int read
  6. {
  7. int x=,f=;char ch=getchar();
  8. while(ch<''||ch>'') {if(ch=='-')f=-;ch=getchar();}
  9. while(ch>=''&&ch<=''){x=*x+(ch-'');ch=getchar();}
  10. return x*f;
  11. }
  12.  
  13. const int N = ;
  14.  
  15. struct edge{
  16. int v,nxt,w;
  17. }e[N<<];
  18.  
  19. int n,m;
  20. int head[N],size;
  21. int dis[N],vis[N],maxn = INT_MAX;
  22. void add(int u,int v,int w)
  23. {
  24. e[++size].v = v;
  25. e[size].w = w;
  26. e[size].nxt = head[u];
  27. head[u] = size;
  28. }
  29.  
  30. queue<int>q;
  31.  
  32. void spfa(int s)
  33. {
  34. memset(dis,0x3f,sizeof(dis));
  35. memset(head,,sizeof());
  36. memset(vis,,sizeof(vis));
  37. dis[s] = ;
  38. q.push(s);
  39. vis[s] = ;
  40. while(!q.empty())
  41. {
  42. int u = q.front(); q.pop(); vis[u] = ;
  43. for(int i = head[u]; i ; i = e[i].nxt)
  44. {
  45. int v = e[i].v, w = e[i].w;
  46. if(dis[v] > dis[u] + )
  47. {
  48. dis[v] = dis[u] + ;
  49. if(!vis[v])
  50. {
  51. q.push(v);
  52. vis[v] = ;
  53. }
  54. }
  55. }
  56. }
  57. }
  58.  
  59. int main()
  60. {
  61. freopen("treasure.in","r",stdin);
  62. n = read; m = read;
  63. int u,v,w;
  64. for(int i = ; i <= m; i++)
  65. {
  66. u = read; v = read; w = read;
  67. add(u,v,w);
  68. add(v,u,w); }
  69. for(int i = ; i <= n; i++)
  70. {
  71. spfa(i);
  72. int ans = ;
  73. for(int i = ; i <= n; i++)
  74. {
  75. ans += dis[i] * w;
  76. }
  77. maxn = min(maxn , ans);
  78. }
  79. printf("%d",maxn);
  80. return ;
  81. }

第二次尝试 dfs

  1. #include <bits/stdc++.h>
  2. #define read read()
  3. using namespace std;
  4.  
  5. const int N = ;
  6.  
  7. int n,m;
  8. int cost[N][N],cnt[N];
  9. int head[N],size;
  10. int ans = INT_MAX, mincost;
  11. int read
  12. {
  13. int x = ; char ch = getchar();
  14. while(ch < || ch > ) ch = getchar();
  15. while(ch >= && ch <= ) { x = * x + ch - ; ch = getchar();}
  16. return x;
  17. }
  18.  
  19. void dfs(int cur)//暴力枚举所有情况;
  20. {
  21. if(cur == n)
  22. {
  23. ans = min(ans,mincost);
  24. return;
  25. }
  26. if(mincost > ans) return;
  27. for(int i = ; i <= n; i++) //枚举出点;
  28. {
  29. if(!cnt[i]) continue; //还未连通的点不能当作出点;
  30. for(int j = ; j <= n; j++) //枚举入点;
  31. {
  32. if(i == j || cnt[j] || cost[i][j] == 0x3f3f3f3f) continue;
  33. mincost += cnt[i] * cost[i][j];
  34. cnt[j] = cnt[i] + ;
  35. dfs(cur + );
  36. mincost -= cnt[i] * cost[i][j];
  37. cnt[j] = ;
  38. }
  39. }
  40. }
  41.  
  42. int main()
  43. {
  44. // freopen("treasure.in","r",stdin);
  45. n = read;m = read;
  46. memset(cost,0x3f,sizeof(cost));
  47. int u,v;
  48. for(int i = ; i <= m; i++)
  49. {
  50. u = read; v = read;
  51. cost[u][v] = cost[v][u] = min(read , cost[u][v]); //细节: u-v之间可能有多条路连接, 忽略其余选最短;
  52. }
  53. for(int i = ; i <= n; i++) //枚举起点;
  54. {
  55. cnt[i] = ;
  56. dfs();
  57. cnt[i] = ;
  58. }
  59. printf("%d",ans);
  60. return ;
  61. }

NOIP 2017 d2t2 70points的更多相关文章

  1. NOIP 2017 解题报告

    ---恢复内容开始--- NOIP 2017 的题真的很难啊,怪不得当年我这个萌新爆零了(当然现在也是萌新)越学越觉得自己什么都不会. 想要成为强者要把这些好题都弄懂弄透 至少现在6道题我都比较陌生 ...

  2. NOIP 2017 列队 - Splay - 树状数组

    题目传送门 传送点I 传送点II 题目大意 (家喻户晓的题目应该不需要大意) (我之前咋把NOIP 2017打成了NOIP 2018,好绝望) Solution 1 Splay 每行一颗Splay,没 ...

  3. 【游记】NOIP 2017

    时间:2017.11.11~2017.11.12 地点:广东省广州市第六中学 Day1 T1:看到题目,心想这种题目也能放在T1? 这个结论我之前遇到过至少3次,自己也简单证明过.初见是NOIP200 ...

  4. NOIP 2017 小凯的疑惑

    # NOIP 2017 小凯的疑惑 思路 a,b 互质 求最大不能表示出来的数k 则k与 a,b 互质 这里有一个结论:(网上有证明)不过我是打表找的规律 若 x,y(设x<y) 互质 则 : ...

  5. 历年真题 未完成(Noip 2008 - Noip 2017)

    Noip 2008 :全部 Noip 2009 :全部 Noip 2010 :AK Noip 2011 :AK Noip 2012 : Vigenère 密码,国王游戏,开车旅行 Noip 2013 ...

  6. 「NOIP 2017」列队

    题目大意:给定一个 $n times m$ 的方阵,初始时第 $i$ 行第 $j$ 列的人的编号为 $(i-1) times m + j$,$q$ 次给出 $x,y$,让第 $x$ 行 $y$ 列的人 ...

  7. NOIP 2016 D2T2 蚯蚓](思维)

    NOIP 2016 D2T2 蚯蚓 题目大意 本题中,我们将用符号 \(\lfloor c \rfloor⌊c⌋\) 表示对 \(c\) 向下取整,例如:\(\lfloor 3.0 \rfloor = ...

  8. 洛谷 P3951 NOIP 2017 小凯的疑惑

    洛谷 P3951 NOIP 2017 小凯的疑惑 题目描述 小凯手中有两种面值的金币,两种面值均为正整数且彼此互素.每种金币小凯都有 无数个.在不找零的情况下,仅凭这两种金币,有些物品他是无法准确支付 ...

  9. NOIP 2017 提高组 day1t2 时间复杂度

    P3952 时间复杂度 标签 NOIp提高组 2017 时空限制 1000ms / 128MB 小明正在学习一种新的编程语言 A++,刚学会循环语句的他激动地写了好多程序并 给出了他自己算出的时间复杂 ...

随机推荐

  1. eclipse中没有tomcat小猫

    安装了tomcat,按网上的说明也使用了tomcatPluginV331 配置文件,还是没有小猫,后来我发现,网上的tomcatPluginV331 针对eclipse 4.4版本,所以应该是插件的版 ...

  2. WAS 常见报错

    1) An error occurred while deleting the server. ADMG0011E: An unexpected exception occurred com.ibm. ...

  3. macOS 升级后重装命令行工具的问题

    问题背景 最近升级个人macbook 从 10.13 到 10.14 在终端输入 git 不能用了,发现是重装操作系统后原来的 Command Line Tools 被自动卸载了, 采用 xcode- ...

  4. linux 备忘录

    1. ps aux|grep 程序 -------->查看当前程序是否运行 ps aux|grep nginx 2. tar -zxvf 压缩包 ---------> 解压缩 tar -z ...

  5. Java遍历文件夹下的所以文件

    利用Java递归遍历文件夹下的所以文件,然后对文件进行其他的操作.如:对文件进行重命名,对某一类文件进行重编码.可以对某一工程下的全部.java文件进行转码成utf-8等 代码如下,这里只对文件进行重 ...

  6. 从mysql导入及导出csv

    csv导入: load data local infile 'D:/pcode/shu/data/a.csv' into table a fields terminated by ','; csv导出 ...

  7. Linux 任务管理 && 常用指令

    A.linux死机 转自:https://www.deleak.com/blog/2010/10/20/sysrq/ linux死机了怎么办? 曾经啊,对着键盘上 Print Screen/SysRq ...

  8. Windows下PythonQt编译(vs2015+Qt5.11.2+PythonQt 3.2)

    后记: 由于自己low,没有下载罪行的python3.2导致编译上遇到种种问题,后文可以参考,建议看: <Windows7 VS2015 下编译 PythonQt3.2> https:// ...

  9. c# 多个事件公用一个相应方法判断事件来源

    假设下边的相应方法有多个事件共同使用.根据事件的sender 判断来源,做相应的处理 假设事件来源DataManSystem;private void OnSystemConnected(object ...

  10. 1C - A + B Problem II

    I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum o ...