bzoj1003题解

【题意分析】

  给你一张无向图,固定起点和终点,除这两点外每个点都有可能消失一段时间(保证起点和终点相互可达),每天选择的路径总长,以及对路径的修改都有代价,求给定时间内最小代价保证起点终点始终连通。

【解题思路】

  此题数据范围极小,可直接用最短路+DP水过。

  先预处理cost[i][j]表示从第i天到第j天都选择一样的路径的最小代价,直接用Dijkstra或SPFA即可。时间复杂度O(N2Mlog2M)或O(N2kE)。

  然后DP,f[i]表示从第1天到第i天的最小代价,边界f[0]=-K,转移方程f[i]=f[j]+cost[j+1][i]*(i-j)+K(j<i)。时间复杂度O(N2)。

  总时间复杂度O(N2Mlog2M)或O(N2kE)。

【参考代码】

  1 #include <cctype>
  2 #include <cstdio>
  3 #define REP(I,start,end) for(int I=(start);I<=(end);I++)
  4 #define PER(I,start,end) for(int I=(start);I>=(end);I--)
  5 #define maxint 32767
  6 #define maxlongint 2147483647
  7 typedef long long LL;
  8 inline int getint()
  9 {
 10     char ch=getchar();
 11     for(;!isdigit(ch)&&ch!=‘-‘;ch=getchar());
 12     bool impositive=ch==‘-‘;
 13     if(impositive)
 14         ch=getchar();
 15     int result=0;
 16     for(;isdigit(ch);ch=getchar())
 17         result=(result<<3)+(result<<1)+ch-‘0‘;
 18     return impositive?-result:result;
 19 }
 20 inline LL getLL()
 21 {
 22     char ch=getchar();
 23     for(;!isdigit(ch)&&ch!=‘-‘;ch=getchar());
 24     bool impositive=ch==‘-‘;
 25     if(impositive)
 26         ch=getchar();
 27     LL result=0ll;
 28     for(;isdigit(ch);ch=getchar())
 29         result=(result<<3)+(result<<1)+ch-‘0‘;
 30     return impositive?-result:result;
 31 }
 32 template<typename T> inline bool getmax(T &target,T pattern)
 33 {
 34     return pattern>target?target=pattern,true:false;
 35 }
 36 template<typename T> inline bool getmin(T &target,T pattern)
 37 {
 38     return pattern<target?target=pattern,true:false;
 39 }
 40 //Header Template
 41 #include <cstring>
 42 using namespace std;
 43 bool nownot[30],used[30],cannot[110][30];
 44 int n,rest[30],dist[30],f[110],map[30][30],cost[110][110];
 45 inline int Dijkstra()
 46 {
 47     int cnt=0;
 48     REP(i,1,n)
 49         if(!nownot[i])
 50             rest[++cnt]=i;
 51     memset(dist,0x3f,sizeof(dist));
 52     memset(used,0,sizeof(used));
 53     dist[1]=0;
 54     REP(i,2,cnt)
 55     {
 56         int miner=maxlongint,mini;
 57         REP(j,1,cnt)
 58             if(!used[j]&&getmin(miner,dist[j]))
 59                 mini=j;
 60         used[mini]=true;
 61         REP(j,1,cnt)
 62             if(!used[j])
 63                 getmin(dist[j],dist[mini]+map[rest[mini]][rest[j]]);
 64     }
 65     return dist[cnt]<dist[0]?dist[cnt]:-1;
 66 }
 67 int main()
 68 {
 69     int day=getint();
 70     n=getint();
 71     int K=getint(),m=getint();
 72     memset(map,0x3f,sizeof(map));
 73     while(m--)
 74     {
 75         int u=getint(),v=getint(),l=getint();
 76         map[u][v]=map[v][u]=l;
 77     }
 78     int d=getint();
 79     memset(cannot,0,sizeof(cannot));
 80     while(d--)
 81     {
 82         int p=getint(),start=getint(),end=getint();
 83         REP(i,start,end)
 84             cannot[i][p]=true;
 85     }
 86     REP(i,1,day)
 87     {
 88         memset(nownot,0,sizeof(nownot));
 89         REP(j,i,day)
 90         {
 91             REP(k,1,n)
 92                 nownot[k]|=cannot[j][k];
 93             cost[i][j]=Dijkstra();
 94         }
 95     }
 96     memset(f,0x7f,sizeof(f));
 97     f[0]=-K;
 98     REP(i,1,day)
 99         REP(j,0,i-1)
100         {
101             int c=cost[j+1][i];
102             if(c>=0)
103                 getmin(f[i],f[j]+cost[j+1][i]*(i-j)+K);
104         }
105     printf("%d\n",f[day]);
106     return 0;
107 }
时间: 2024-08-15 04:48:42

bzoj1003题解的相关文章

洛谷 P1079 Vigen&#232;re 密码 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:https://www.luogu.org/problem/show?pid=1079 题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密 码.Vigenère 密码的加密解密算法简单易用,且破译难度比较高,曾在美国南北战争中为 南军所广泛使用. 在密码学中,我们称需要加密的信息为明文,用 M 表示:称加密后的信息为密文,用 C 表示:而密钥是一种

8.8联考题解

今天的T1让我怀疑我是不是在做奥赛题--这考的是什么知识点啊这个,会不会用绝对值函数? Evensgn 的债务 时间限制: 1 Sec  内存限制: 128 MB 题目描述 Evensgn 有一群好朋友,他们经常互相借钱.假如说有三个好朋友A,B,C.A 欠 B 20 元,B 欠 C 20 元,总债务规模为 20+20=40 元.Evensgn 是个追求简约的人,他觉得这样的债务太繁杂了.他认为,上面的债务可以完全等价为 A 欠C20 元,B 既不欠别人,别人也不欠他.这样总债务规模就压缩到了 

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

2017ZZUACM省赛选拔试题部分题解----谨以纪念我这卡线滚粗的美好经历

写在前面: 其实心里有些小小的不爽又有点小小的舒畅,为啥捏?不爽当然是因为没被选拔上啦,舒畅捏则是因为没被选拔上反而让自己警醒,学长也提点很多很多."沉下去,然后一战成名"学长如是对我说,我很开心.其实这完全算不算是题解,只是我个人的一些小想法而已.而且到现在还有一题不会...让自己长点记性吧. 题目 A :聪明的田鼠 Time Limit: 1 Sec Memory Limit: 128 MB Description 田鼠MIUMIU来到了一片农田,农田可以看成是一个M*N个方格的矩

LeetCode-001题解

此题目摘自LeetCode001 Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.

leetcode题解: Next Permutation

最近还一直在刷leetcode,当然,更多时候只是将题解写在自己的电脑上,没有分享出来.偶尔想起来的时候,就写出来. public class Solution { public void nextPermutation(int[] nums) { if(nums==null||nums.length<=1) return; nextPermutationHelp( nums,0,nums.length-1); } public void nextPermutationHelp(int []nu

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

HDU 1045 Fire Net 二分图Bipartite题解

本题可以使用DFS直接爆搜出答案,不过这样类型的题目其实是个二分图的题解. 这个二分图,难不在Hungary算法,而是难在于建图.需要挺高的抽象思维的. 建图: 1 把同一行不被X分开的格子标同一个号码,被X分开的标下一个号码,这样做是为了缩点,不需要把所有的格子都分开标号,而且可以更方便建个更加小的图. 2 同理把同一列的格子标号 3 然后判断相同一个格子的行标号和列标号是有路径的,其他不在同一个格子的都是没有路径的. 4 这样就等于以行标号和列标号作为左右顶点,构建成一个二分图了 然后使用H