8.16总结
一、比赛基本情况
比赛日期: 8.16
比赛时长: 120
比赛得分: 250
比赛排名:1
二、比赛整体回顾
做题顺序:1-2-4-3
时间安排:
T1:60min
T2:15min
T3:10min
T4:35min
比赛状态:感觉很简单
整体感受:感觉很简单
三、题目复盘
P1077 [NOIP 2012 普及组] 摆花
本题得分:30
初始思路:
本来以为是排列组合,结果写的时候错了,所以只打了遍dfs。
考场代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define ull unsigned long long
using namespace std;
const int N=2000020,B=13131;
int n,m;
int ans=0;
int a[N];
vector<int>q;
void dfs(int x,int sy)
{
if(x>n)
{
if(sy==0)ans++;
return ;
}
for(int i=0;i<=a[x];i++)
{
q.push_back(i);
dfs(x+1,sy-i);
q.pop_back();
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
dfs(1,m);
cout<<ans%1000007;
return 0;
}
/*
20 20
1 6 3 7 5 8 6 7 2 6 3 7 5 8 6 9 3 5 6 7
*/
正确思路:
这是一个方案数类型多重背包
背包容量:m
物品个数:n
重量:1
限制:a[i];
正确代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define ull unsigned long long
using namespace std;
const int N=2000020,mod=1e6+7;
int n,s[N],dp[N];
int m;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>s[i];
}
dp[0]=1;
for(int i=1;i<=n;i++)
{
for(int j=m;j>=1;j--)
{
for(int k=1;k<=j&&k<=s[i];k++)
{
dp[j]=dp[j]+dp[j-k];
dp[j]%=mod;
}
}
}
cout<<dp[m]%mod;
return 0;
}
P4822 [BJWC2012] 冻结
本题得分:20
初始思路:
由于后面没时间了,所以只判断了所有边的时间都可以减半,直接算出1到n的最短路再/2即可。
考场代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define ull unsigned long long
using namespace std;
const int N=2000020,B=13131;
int dp[60][60];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,m,cnt;
cin>>n>>m>>cnt;
memset(dp,0x3f,sizeof dp);
for(int i=1;i<=n;i++)
{
dp[i][i]=0;
}
for(int i=1;i<=m;i++)
{
int u,v,w;
cin>>u>>v>>w;
dp[u][v]=min(dp[u][v],w);
dp[v][u]=min(dp[v][u],w);
}
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
}
}
}
cout<<dp[1][n]/2;
return 0;
}
正确思路:
本题要用机会型dp的思想来写。
定义状态:
dis[i][j],走到i位置用掉j次机会的最短时间。
答案:所有的dp[n][i];
方程:
不使用:dis[v][jh]=dis[tmp][jh]+w;
使用: if(dis[v][jh+1]>dis[tmp][jh]+w/2 && jh+1<=k)
dis[v][jh+1]=dis[tmp][jh]+w/2;
边界条件:dp[st][0]=0;
正确代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=10020;
struct node
{
int v,w,ji;
bool operator<(node b)const
{
return w>b.w;
}
};
vector<node>g[N];
int dis[10010][60];
int vis[10010][60];
int n,m,k;
void dj(int x)
{
priority_queue<node>q;
memset(dis,0x3f,sizeof dis);
q.push({x,0,0});
dis[x][0]=0;
while(q.size()>0)
{
int jh=q.top().ji;
int tmp=q.top().v;
q.pop();
if(vis[tmp][jh]==1)continue;
vis[tmp][jh]=1;
for(auto e:g[tmp])
{
int v=e.v;
int w=e.w;
if(dis[v][jh]>dis[tmp][jh]+w)
{
dis[v][jh]=dis[tmp][jh]+w;
q.push({v,dis[v][jh],jh});
}
if(dis[v][jh+1]>dis[tmp][jh]+w/2 && jh+1<=k)
{
dis[v][jh+1]=dis[tmp][jh]+w/2;
q.push({v,dis[v][jh+1],jh+1});
}
}
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>k;
for(int i=1;i<=m;i++)
{
int u,v,w;
cin>>u>>v>>w;
g[u].push_back({v,w,0});
g[v].push_back({u,w,0});
}
dj(1);
int mini=1e18;
for(int i=0;i<=k;i++)
{
mini=min(mini, dis[n][i]);
}
cout<<mini;
return 0;
}