第一题

用堆维护。

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <queue>
  4. int n,i,f[400000],g[2][200000],j=0,k[400000];
  5. int l,r;
  6. bool cho;
  7. struct pn{
  8. int l,r,n;
  9. };
  10. bool operator<(pn a,pn b){
  11. return a.n>b.n;
  12. }
  13. std::priority_queue<pn> q;
  14. pn as,as1,as2;
  15. int main(){
  16. freopen("minval.in","r",stdin);
  17. freopen("minval.out","w",stdout);
  18. scanf("%d",&n);
  19. for(i=0;i<n;++i) scanf("%d",&g[0][i]);
  20. for(i=0;i<n;++i) scanf("%d",&g[1][i]);
  21. std::sort(g[0],g[0]+n);
  22. std::sort(g[1],g[1]+n);
  23. as.l=0;
  24. as.r=0;
  25. as.n=g[0][0]+g[1][0];
  26. q.push(as);
  27. for(i=0;i<n;++i){
  28. as=q.top();
  29. q.pop();
  30. as1.l=as.l+1;
  31. as1.r=as.r;
  32. as1.n=g[0][as1.l]+g[1][as1.r];
  33. as2.l=as.l;
  34. as2.r=as.r+1;
  35. as2.n=g[0][as2.l]+g[1][as2.r];
  36. printf("%d ",as.n);
  37. q.push(as1);
  38. q.push(as2);
  39. }
  40. return 0;
  41. }

第二题

连通性判定+二分图

  1. #include <cstdio>
  2. #include <cstring>
  3. int f[200000],d,i,j,m,n,s,t,tt,a,b,pl,h[200000];
  4. int q[200000],qh,qt;
  5. bool f2;
  6. struct edge{
  7. int t,n;
  8. } edges[2000000];
  9. inline void addedge(int f,int t){
  10. edges[++pl].n=h[f];
  11. edges[ pl ].t= t ;
  12. h[f]=pl;
  13. }
  14. int main(){
  15. freopen("catch.in","r",stdin);
  16. freopen("catch.out","w",stdout);
  17. scanf("%d",&tt);
  18. while(tt--){
  19. ++j;
  20. memset(f,0,sizeof f);
  21. memset(h,0,sizeof h);
  22. pl=0;
  23. qh=qt=0;
  24. scanf("%d%d%d",&n,&m,&s);
  25. for(i=0;i<m;++i){
  26. scanf("%d%d",&a,&b);
  27. addedge(a,b);
  28. addedge(b,a);
  29. }
  30. q[qt++]=s;
  31. f2=false;
  32. f[s]=1;
  33. --n;
  34. while(qh!=qt){
  35. i=q[qh++];
  36. for(d=h[i];d;d=edges[d].n){
  37. t=edges[d].t;
  38. if(!f[t]){
  39. f[t]=3-f[i];
  40. q[qt++]=t;
  41. --n;
  42. }else{
  43. if(f[i]==f[t]) f2=true;//not bicolorable
  44. }
  45. }
  46. }
  47. if(!f2){
  48. printf("Case %d: NO\n",j);
  49. }else{
  50. if(n) printf("Case %d: NO\n",j); else printf("Case %d: YES\n",j);
  51. }
  52. }
  53. return 0;
  54. }

第三題:二分答案加判定

  1. #include <cstdio>
  2. #include <cstring>
  3. struct edge{
  4. int t,n,w;
  5. } e[300000];
  6. int h[20000],pl;
  7. inline void addedge(int f,int t,int w){
  8. e[++pl].t=t;
  9. e[pl].n=h[f];
  10. e[pl].w=w;
  11. h[f]=pl;
  12. }
  13. int n,m,u,v,s,i,j,k;
  14. int left,right,mid,ans;
  15. int cost[20000];
  16. int q[100000],qh,qt;
  17. bool iq[20000];
  18. int f[20000];
  19. int ka,kb,ww;
  20. int v0,v1,e0;
  21. void spfa(int mma){
  22. memset(f,-1,sizeof f);
  23. memset(iq,0,sizeof iq);
  24. qh=qt=0;
  25. q[qt++]=u;
  26. f[u]=0;
  27. while(qh!=qt){
  28. v0=q[qh++];
  29. iq[v0]=false;
  30. for(e0=h[v0];e0;e0=e[e0].n){
  31. v1=e[e0].t;
  32. if(cost[v1]>mma) continue;
  33. if(f[v1]==-1 || f[v1] > f[v0] + e[e0].w){
  34. f[v1] = f[v0] + e[e0].w;
  35. if(!iq[v1]){
  36. iq[v1]=true;
  37. q[qt++]=v1;
  38. }
  39. }
  40. }
  41. }
  42. }
  43. int main(){
  44. freopen("cost.in","r",stdin);
  45. freopen("cost.out","w",stdout);
  46. scanf("%d%d%d%d%d",&n,&m,&u,&v,&s);
  47. left=0x7fffffff;
  48. for(i=1;i<=n;++i){
  49. scanf("%d",cost+i);
  50. if(cost[i]<left) left=cost[i];
  51. if(cost[i]>right) right=cost[i];
  52. }
  53. for(i=1;i<=m;++i){
  54. scanf("%d%d%d",&ka,&kb,&ww);
  55. addedge(ka,kb,ww);
  56. addedge(kb,ka,ww);
  57. }
  58. spfa(right);
  59. if(f[v]==-1 || f[v] > s){
  60. printf("-1\n");
  61. return 0;
  62. }
  63. while(left<=right){
  64. mid=(left+right)/2;
  65. spfa(mid);
  66. if( f[v] == -1 || f[v] > s ){
  67. left=mid+1;
  68. }else{
  69. right=mid-1;
  70. ans=mid;
  71. }
  72. }
  73. printf("%d\n",ans);
  74. return 0;
  75. }

更新:修復了exceed 32bit int的bug

  1. #include <cstdio>
  2. #include <cstring>
  3. struct edge{
  4. int t,n,w;
  5. } e[300000];
  6. long long h[20000],pl;
  7. inline void addedge(int f,int t,int w){
  8. e[++pl].t=t;
  9. e[pl].n=h[f];
  10. e[pl].w=w;
  11. h[f]=pl;
  12. }
  13. long long n,m,u,v,s,i,j,k;
  14. long long left,right,mid,ans;
  15. long long cost[20000];
  16. long long q[100000],qh,qt;
  17. bool iq[20000];
  18. long long f[20000];
  19. long long ka,kb,ww;
  20. long long v0,v1,e0;
  21. void spfa(int mma){
  22. memset(f,-1,sizeof f);
  23. memset(iq,0,sizeof iq);
  24. qh=qt=0;
  25. q[qt++]=u;
  26. f[u]=0;
  27. while(qh!=qt){
  28. v0=q[qh++];
  29. iq[v0]=false;
  30. for(e0=h[v0];e0;e0=e[e0].n){
  31. v1=e[e0].t;
  32. if(cost[v1]>mma) continue;
  33. if(f[v1]==-1 || f[v1] > f[v0] + e[e0].w){
  34. f[v1] = f[v0] + e[e0].w;
  35. if(!iq[v1]){
  36. iq[v1]=true;
  37. q[qt++]=v1;
  38. }
  39. }
  40. }
  41. }
  42. }
  43. int main(){
  44. freopen("cost.in","r",stdin);
  45. freopen("cost.out","w",stdout);
  46. scanf("%lld%lld%lld%lld%lld",&n,&m,&u,&v,&s);
  47. left=0x7fffffff;
  48. for(i=1;i<=n;++i){
  49. scanf("%lld",cost+i);
  50. if(cost[i]<left) left=cost[i];
  51. if(cost[i]>right) right=cost[i];
  52. }
  53. for(i=1;i<=m;++i){
  54. scanf("%lld%lld%lld",&ka,&kb,&ww);
  55. addedge(ka,kb,ww);
  56. addedge(kb,ka,ww);
  57. }
  58. spfa(right);
  59. if(f[v]==-1 || f[v] > s){
  60. printf("-1\n");
  61. return 0;
  62. }
  63. while(left<=right){
  64. mid=(left+right)/2;
  65. spfa(mid);
  66. if( f[v] == -1 || f[v] > s ){
  67. left=mid+1;
  68. }else{
  69. right=mid-1;
  70. ans=mid;
  71. }
  72. }
  73. printf("%lld\n",ans);
  74. return 0;
  75. }

  

15 day 1代碼的更多相关文章

  1. 配置editplus,讓其支持代碼自動格式化功能.

    使用editplus已經好多年了,累積了不少的東西,想換IDE比較麻煩,所以就研究了一下用editplus搭配gofmt.exe配置go語言代碼自動格式化的功能.還好功夫不負有心人,終於被我搞懂了,不 ...

  2. 我用了13行代碼開發出来的PHP框架

    我只用13行代碼開發的PHP框架,如果您對框架不理解,不知道框架究竟幫您做了什麽事,可以下載此框架看一下, 另外如果您想開發自己的框架也可以由這個框架的思路進行擴展. 源碼下載地址:http://do ...

  3. 关于ios 8 7 下的模态窗口大小的控制 代碼+場景(mainstoryboard)( Resizing UIModalPresentationFormSheet )

    1 代碼 UIViewController* modalController = [[UIViewController alloc]init];modalController.modalTransit ...

  4. phper談談最近重構代碼的感受(1)

    作爲一個工作時間並不算長的phper,卻參與了兩家公司的代碼重構.下面談談我的一些感受. 在mjm公司,當時我負責日常的需求開發和2.0的重構.當初的重構更多的是clean codes和一些代碼規範上 ...

  5. ruby簡單的代碼行統計工具

    看代码 # encoding: utf-8 class CodeLineStat attr_reader :code_lines def initialize @code_lines = 0 end ...

  6. Mybatis逆向生成代碼

    Idea 单模块 1.在pom.xml中添加依赖 <build> <plugins> <plugin> <groupId>org.mybatis.gen ...

  7. 華氏溫度轉化為攝氏溫度的簡單JavaScript代碼

    今天,跟著W3School學到了"JavaScript函數",代碼都挺簡單的,在運算符調用函數的地方寫了一個小程序.原碼程序是這樣的: <!DOCTYPE html> ...

  8. IO流簡單代碼

    今天測試了一下,在博客園裏HTML源碼編譯器裏寫CSS内部樣式,更新編譯后,内部樣式可用,但是會將寫的内部樣式代碼强制加上代碼注釋進行編譯,有點類似于强制注入.編譯后的效果就是在前面加入了一個空的p標 ...

  9. [個人紀錄] WindowsLiveWriter 插入代碼跳出錯誤

    跳出找不到設定檔Can’t load configruaration fromC:\Users\…\AppData\Roaming\Windows Live Writer\WindowsLiveWri ...

随机推荐

  1. 视频播放实时记录日志并生成XML文件

    需求描述: 在JWPlayer视频播放过程中,要求实时记录视频观看者播放.暂停的时间,并记录从暂停到下一次播放时所经过的时间.将所有记录保存为XML文件,以方便数据库的后续使用. 实现过程: 尝试1: ...

  2. Docker-2 的创建、启动、终止、删除、迁移等

    学习博客地址:http://www.dwhd.org/20151115_140935.html

  3. JMeter工具的使用-ForEach

    1,Add Thread group this detail information about this panel as below link http://jmeter.apache.org/u ...

  4. grunt使用watch和livereload的Gruntfile.js的配置

    周末在家看angularJS, 用grunt的livereload的自动刷新, 搞了大半天, 现在把配置贴出来, 免得以后忘记了, 只要按照配置一步步弄是没有问题的; 开始的准备的环境安装是: (1) ...

  5. 【CodeForces 626E】Simple Skewness

    题意 给出n个数的集合,求一个 (平均数-中位数)最大 (偏度最大)的子集,输出子集元素个数和各个元素(任意顺序). 分析 因为是子集,所以不一定是连续的序列.然后我们有下面几个结论. 1.最大偏度一 ...

  6. 【CodeForces 471A】MUH and Sticks

    题 题意 给你六根木棍的长度,熊需要头比身体短,大象需要头和身体一样,四肢要一样长,否则就是外星人.请你判断能组成哪一个. 分析 暴力,循环看一下每根有几根相同的,然后如果有四根都是有四根相同的&am ...

  7. Yii2修改默认布局

    public $layout = 'layout';//在类中定义一个变量,名为$layout的php文件 <?php echo $content; ?>

  8. 常用sql,在做项目时用mysqlWorkBeach里面自动生成的

    -- 修改表中的字段的长度ALTER TABLE `sfkbbs`.`sfk_father_module` CHANGE ) NULL DEFAULT NULL COMMENT '父板块名字' ; 在 ...

  9. jauery加入项目中,但是在页面中显示没有找到这个文件--springMVC框架

    遇到一件很不爽的事情,自己明明已经把jquery的文件放在了项目中,但是在页面中总是看不到效果,开发者模式提示没有找到文件,当时都要郁闷疯了,后来无意间看到了Eclipse中报的错,怎么与Spring ...

  10. c中动态使用数组

    #include <iostream> #include <fstream> #include<stdlib.h> #define MAXNUM 200 int I ...