In Action

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5102    Accepted Submission(s):
1696

Problem Description

Since 1945, when the
first nuclear bomb was exploded by the Manhattan Project team in the US, the
number of nuclear weapons have soared across the globe.
Nowadays,the crazy
boy in FZU named AekdyCoin possesses some nuclear weapons and wanna destroy our
world. Fortunately, our mysterious spy-net has gotten his plan. Now, we need to
stop it.
But the arduous task is obviously not easy. First of all, we know
that the operating system of the nuclear weapon consists of some connected
electric stations, which forms a huge and complex electric network. Every
electric station has its power value. To start the nuclear weapon, it must cost
half of the electric network's power. So first of all, we need to make more than
half of the power diasbled. Our tanks are ready for our action in the base(ID is
0), and we must drive them on the road. As for a electric station, we control
them if and only if our tanks stop there. 1 unit distance costs 1 unit oil. And
we have enough tanks to use.
Now our commander wants to know the minimal oil
cost in this action.
 
Input
The first line of the input contains a single integer
T, specifying the number of testcase in the file.
For each case, first line
is the integer n(1<= n<= 100), m(1<= m<= 10000), specifying the
number of the stations(the IDs are 1,2,3...n), and the number of the roads
between the station(bi-direction).
Then m lines follow, each line is interger
st(0<= st<= n), ed(0<= ed<= n), dis(0<= dis<= 100), specifying
the start point, end point, and the distance between.
Then n lines follow,
each line is a interger pow(1<= pow<= 100), specifying the electric
station's power by ID order.
 
Output
The minimal oil cost in this action.
If not exist
print "impossible"(without quotes).
 
Sample Input
2
2 3
0 2 9
2 1 3
1 0 2
1
3
2 1
2 1 3
1
3
 
Sample Output
5
impossible
 
Author
Lost@HDU
 
Source
 
Recommend
lcy   |   We have carefully selected several similar
problems for you:  1385 3338 1102 3342 1598 
 
先用迪杰斯特拉求出从0点到各点的最短距离,由于需要大于一半总能量,所以需要判断是否取这个点,使用01背包解决。
 
题意:第一个数为数据组数,然后输入n,m,代表有n个发电站,m条路,然后给出m条路和该路消耗的油,然后是n行,每行是相应的发电站的能量,要注意,每个发电站必须停一辆坦克才能控制这个发电站,只有控制的总能量超过一半能使其瘫痪
 
附上代码:
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#define M 105
#define inf 0x3f3f3f3f
using namespace std;
int map[M][M],dis[M],vis[M];
int dp[];
int mins(int a,int b)
{
return a>b?b:a;
}
int main()
{
int i,j,n,m,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(dis,,sizeof(dis));
memset(vis,,sizeof(vis));
for(i=; i<=n; i++)
for(j=; j<=n; j++)
if(i==j) map[i][j]=;
else map[i][j]=inf;
int a,b,c;
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
if(map[a][b]>c)
map[a][b]=map[b][a]=c;
}
vis[]=;
for(i=; i<=n; i++)
dis[i]=map[][i];
int min,t;
for(i=; i<=n; i++) //迪杰斯特拉
{
min=inf;
for(j=; j<=n; j++)
if(!vis[j]&&min>dis[j])
{
min=dis[j];
t=j;
}
vis[t]=;
for(j=; j<=n; j++)
if(!vis[j]&&map[t][j]<inf)
if(dis[j]>dis[t]+map[t][j])
dis[j]=dis[t]+map[t][j];
}
int s[M],sum=;
for(i=; i<=n; i++)
{
scanf("%d",&s[i]);
sum+=s[i];
}
for(i=; i<=sum; i++)
dp[i]=inf;
dp[]=;
for(i=; i<=n; i++) //01背包
for(j=sum; j>=s[i]; j--)
dp[j]=mins(dp[j],dp[j-s[i]]+dis[i]);
int x=sum/+,mm=inf;
for(i=x; i<=sum; i++) //选出最小的距离
if(dp[i]<mm)
mm=dp[i];
if(mm<inf)
printf("%d\n",mm);
else
printf("impossible\n");
}
return ;
}

hdu 3339 In Action(迪杰斯特拉+01背包)的更多相关文章

  1. HDU 3339 In Action(迪杰斯特拉+01背包)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3339 In Action Time Limit: 2000/1000 MS (Java/Others) ...

  2. HDU 3339 In Action【最短路+01背包】

    题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=3339] In Action Time Limit: 2000/1000 MS (Java/Other ...

  3. HDU 3339 In Action【最短路+01背包模板/主要是建模看谁是容量、价值】

     Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the n ...

  4. HDU 2680 最短路 迪杰斯特拉算法 添加超级源点

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. HDU 2544最短路 (迪杰斯特拉算法)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others)    Me ...

  6. HDU 3790(两种权值的迪杰斯特拉算法)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3790 最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    ...

  7. HDU 1874畅通工程续(迪杰斯特拉算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)     ...

  8. hdu 1142(迪杰斯特拉+记忆化搜索)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  9. hdu 1595 find the longest of the shortest(迪杰斯特拉,减去一条边,求最大最短路)

    find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 ...

随机推荐

  1. 洛谷P1514 [NOIP2010提高组T4]引水入城

    P1514 引水入城 题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城 ...

  2. 洛谷P2258 子矩阵[2017年5月计划 清北学堂51精英班Day1]

    题目描述 给出如下定义: 子矩阵:从一个矩阵当中选取某些行和某些列交叉位置所组成的新矩阵(保持行与列的相对顺序)被称为原矩阵的一个子矩阵. 例如,下面左图中选取第2.4行和第2.4.5列交叉位置的元素 ...

  3. CWnd::Attach()具体解释

    CWnd::Attach Attaches a Windows window to a CWnd object. BOOL Attach(    HWND hWndNew ); Parameters ...

  4. Effective Modern C++:08调整

    41:针对可复制的形参,在移动成本低且一定会被复制的前提下,考虑将其按值传递 class Widget { public: void addName(const std::string& ne ...

  5. MaxCompute技术人背后的故事:从ApacheORC到AliORC

    2019大数据技术公开课第一季<技术人生专访>来袭,本季将带领开发者们探讨大数据技术,分享不同国家的工作体验.本文整理自阿里巴巴计算平台事业部高级技术专家吴刚的专访,将为大家介绍Apach ...

  6. BOT建设经营转让,PPP公私合作

    PPP.BOT两种模式有什么区别? BOT模式(build-operate-transfer),由投资方建设并专营一定期限最后移交政府的方式:PPP模式(public-private-partners ...

  7. LintCode_167 链表求和

    题目 你有两个用链表代表的整数,其中每个节点包含一个数字.数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头.写出一个函数将两个整数相加,用链表形式返回和. 样例 给出两个链表 3-&g ...

  8. 2019-1-29-jekyll-如何加密博客-防止抓取

    title author date CreateTime categories jekyll 如何加密博客 防止抓取 lindexi 2019-01-29 16:26:17 +0800 2018-2- ...

  9. python 通过.pth文件修改搜索路径

  10. padas操作

    1.从excel读取数据 pd.read_excel('naifen.xlsx') 2.保存为excel pd.to_excel('bb.xlsx') 3.统计某一列重复数据 df.groupby([ ...