[HDU3339] [HDUMonthly0306] In Action [floyd][01揹包]
阿新 • • 發佈:2018-11-09
題意:
給一張 點邊無向圖,每個點有一個權值 。
現在要求從點開多隻坦克,停在某些點使得覆蓋點
求所有坦克開過的距離和的最小值。
還算勉強的拼題吧
跑一遍floyd然後01揹包 dij也行啦
當然跑dij,spfa之類的也可以
一開始揹包搞反了還行8
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define add_edge(a,b,c) nxt[++tot]=head[a],head[a]=tot,to[tot]=b,val[tot]=c
#define PII pair<int,int>
priority_queue<PII,vector<PII>,greater<PII> >Q;
int T,N,M,tot=0,sum=0,ans=0x3f3f3f3f;
int head[105]={},nxt[20005]={},to[20005]={},val[20005]={};
int id[105][105]={};
int f[2000005]={};
int dis[105]={},power[105]={};
bool vis[105]={};
void dijkstra()
{
Q.push(make_pair(0,0));
while(!Q.empty())
{
int x=Q.top().second; Q.pop(); vis[x]=1;
for(int i=head[x];i;i=nxt[i])
{
if(dis[to[i]]>dis[x]+val[i])
{
dis[to[i]]=dis[x]+val[i];
if(!vis[to[i]])Q.push(make_pair(dis[to[i]],to[i]));
}
}
}
}
int main() {
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&M);
memset(head,0,sizeof(head));
memset(vis,0,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
for(int i=1;i<=2000000;++i)f[i]=0x3f3f3f3f;
memset(id,0,sizeof(id));
sum=tot=f[0]=dis[0]=0; ans=0x3f3f3f3f;
for(int a,b,c,i=1;i<=M;++i)
{
scanf("%d%d%d",&a,&b,&c);
if(a==b)continue;
if(id[a][b])val[id[a][b]]=min(val[id[a][b]],c); else add_edge(a,b,c),id[a][b]=tot;
if(id[b][a])val[id[b][a]]=min(val[id[b][a]],c); else add_edge(b,a,c),id[b][a]=tot;
}
for(int i=1;i<=N;++i)scanf("%d",&power[i]),sum+=power[i];
dijkstra();
for(int i=1;i<=N;++i)
for(int j=sum;j>=power[i];--j)
{
if(!vis[i])continue; //!!!!!!!!!!!!!
f[j]=min(f[j],f[j-power[i]]+dis[i]);
}
for(int i=sum;i>sum/2;--i)ans=min(ans,f[i]);
if(ans==0x3f3f3f3f)printf("impossible\n");
else printf("%d\n",ans);
}
return 0;
}
for(int i=1;i<=N;++i)
for(int j=sum;j>=power[i];--j)
f[j]=min(f[j],f[j-power[i]]+dis[i]);
ans=0x3f3f3f3f;
for(int i=sum;i>(sum>>1);--i)ans=min(ans,f[i]);
if(ans==0x3f3f3f3f)puts("impossible");
else printf("%d\n",ans);
}
return 0;
}
data generator
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<cctype>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<queue>
#include<sstream>
using namespace std;
#define ll long long
ll GenRand(const ll Lim1,ll Lim2)
{
++Lim2;
ll ret=Lim1;
int t=0;
while(t<100)
{
if(rand()/(RAND_MAX+1.0)<0.1)break;
ret+=rand();
ret%=Lim2;
++t;
}
while(ret<Lim1)ret+=Lim1;
ret%=Lim2;
return ret;
}
int N,M;
stringstream ss;
int main( int argc, char *argv[] )
{
freopen("data.in","w",stdout);
int seed=time(NULL);
if(argc > 1)//如果有引數
{
ss.clear();
ss<<argv[1];
ss>>seed;//把引數轉換成整數賦值給seed
}
srand(seed);
printf("25\n");
for(int i=1;i<=25;++i)
{
N=GenRand(1,100),M=GenRand(1,10000);
printf("%d %d\n",N,M);
for(int a,b,c,i=1;i<=M;++i)
{
a=GenRand(0,100);
b=GenRand(0,100);
while(a==b)b=GenRand(0,100);
c=GenRand(0,100);
printf("%d %d %d\n",a,b,c);
}
for(int i=1;i<=N;++i)printf("%d\n",GenRand(1,100));
}
return 0;
}
對拍bat↓
@echo off
:loop
data_generator.exe %random% > data.in
std.exe < data.in > std.out
my.exe < data.in > my.out
fc my.out std.out
if not errorlevel 1 goto loop
pause
goto loop