Big Christmas Tree
2015-08-19 16:44
435 查看
Description
Christmas is coming to KCM city. Suby the loyal civilian in KCM city is preparing a big neat Christmas tree. The simple structure of the tree is shown in right picture.
The tree can be represented as a collection of numbered nodes and some edges. The nodes are numbered 1 through
n. The root is always numbered 1. Every node in the tree has its weight. The weights can be different from each other. Also the shape of every available edge between two nodes is different, so the unit price of each edge is different. Because of a
technical difficulty, price of an edge will be (sum of weights of all descendant nodes) × (unit price of the edge).
Suby wants to minimize the cost of whole tree among all possible choices. Also he wants to use all nodes because he wants a large tree. So he decided to ask you for helping solve this task by find the minimum cost.
Input
The input consists of T test cases. The number of test cases T is given in the first line of the input file. Each test case consists of several lines. Two numbers
v, e (0 ≤ v, e ≤ 50000) are given in the first line of each test case. On the next line,
v positive integers wi indicating the weights of
v nodes are given in one line. On the following e lines, each line contain three positive integers
a, b, c indicating the edge which is able to connect two nodes
a and b, and unit price c.
All numbers in input are less than 216.
Output
For each test case, output an integer indicating the minimum possible cost for the tree in one line. If there is no way to build a Christmas tree, print “No Answer” in one line.
Sample Input
Sample Output
题解:其实就是求最短路径,反正我是没读懂,看来别人的感觉和题目说的完全不同。
SPFA(数组邻接表):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const long long INF = 0x3ffffffffffff;
struct Node
{
int to;
int cost;
Node(){
}
Node(int a,int b)
{
to = a;
cost = b;
}
};
int first[50005];
int next[100005];
Node e[100005];
bool visited[50005];
long long d[50005];
long long w[50005];
void spfa(int n)
{
memset(visited,false,sizeof(visited));
for(int i = 1;i <= n;i++)
{
d[i] = INF;
}
d[1] = 0;
queue<int> q;
q.push(1);
visited[1] = true;
while(!q.empty())
{
int x = q.front();
q.pop();
visited[x] = false;
for(int i = first[x];i != -1;i = next[i])
{
int t = e[i].to;
if(d[t] > d[x] + e[i].cost)
{
d[t] = d[x] + e[i].cost;
if(!visited[t])
{
q.push(t);
visited[t] = true;
}
}
}
}
}
int main()
{
int ncase;
cin>>ncase;
while(ncase--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++)
{
first[i] = -1;
scanf("%lld",w + i);
}
int k = 0;
for(int i = 0;i < m;i++)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
e[k].to = v;
e[k].cost = c;
next[k] = first[u];
first[u] = k++;
e[k].to = u;
e[k].cost = c;
next[k] = first[v];
first[v] = k++;
}
if(n == 0 || m == 0)
{
printf("0\n");
continue;
}
spfa(n);
bool flag = true;
long long ans = 0;
for(int i = 1;i <= n;i++)
{
if(d[i] == INF)
{
flag = false;
break;
}
ans += d[i] * w[i];
}
if(flag)
{
printf("%lld\n",ans);
}
else
{
printf("No Answer\n");
}
}
return 0;
}
djistra(数组邻接表):
#i
c5cd
nclude <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const __int64 INF = 10000000000;
struct Node
{
int to;
__int64 cost;
Node(){
}
Node(int a,__int64 b)
{
to = a;
cost = b;
}
bool operator< (Node t) const
{
return cost > t.cost;
}
};
int first[50005];
int next[100005];
Node e[100005];
bool visited[50005];
__int64 d[50005];
__int64 w[50005];
__int64 ans = 0;
void djistra(int n)
{
memset(visited,false,sizeof(visited));
for(int i = 1;i <= n;i++)
{
d[i] = INF;
}
d[1] = 0;
priority_queue<Node> q;
q.push(Node(1,0));
ans = 0;
int cnt = 0;
while(!q.empty())
{
Node p = q.top();
q.pop();
if(visited[p.to])
{
continue;
}
cnt++;
ans += d[p.to] * w[p.to];
visited[p.to] = true;
for(int i = first[p.to];i != -1;i = next[i])
{
int x = e[i].to;
if(!visited[x] && d[x] > d[p.to] + e[i].cost)
{
d[x] = d[p.to] + e[i].cost;
q.push(Node(x,d[x]));
}
}
}
if(cnt != n)
{
ans = -1;
}
}
int main()
{
int ncase;
cin>>ncase;
while(ncase--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++)
{
first[i] = -1;
scanf("%I64d",w + i);
}
int k = 0;
for(int i = 0;i < m;i++)
{
int u,v;
__int64 c;
scanf("%d%d%I64d",&u,&v,&c);
e[k].to = v;
e[k].cost = c;
next[k] = first[u];
first[u] = k++;
e[k].to = u;
e[k].cost = c;
next[k] = first[v];
first[v] = k++;
}
if(n == 0 || n == 1)
{
printf("0\n");
continue;
}
djistra(n);
if(ans != -1)
{
printf("%I64d\n",ans);
}
else
{
printf("No Answer\n");
}
}
return 0;
}
超时vector:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <utility>
using namespace std;
const __int64 INF = 0x3fffffffffff;
struct Node
{
int to;
int cost;
Node(){
}
Node(int a,int b)
{
to = a;
cost = b;
}
bool operator< (Node t) const
{
return cost > t.cost;
}
};
typedef pair<__int64,int>plli;
priority_queue<plli , vector<plli> , greater<plli> > q;
vector<Node> vec[50005];
bool visited[50005];
__int64 d[50005];
int w[50005];
__int64 ans;
void djistra(int n)
{
memset(visited,false,sizeof(visited));
for(int i = 1;i <= n;i++)
{
d[i] = INF;
}
d[1] = 0;
ans = 0;
priority_queue<Node> q;
q.push(Node(1,0));
int cnt = 0;
while(!q.empty())
{
Node p = q.top();
q.pop();
if(visited[p.to])
{
continue;
}
ans += w[p.to] * d[p.to];
cnt++;
visited[p.to] = true;
for(int i = 0;i < vec[p.to].size();i++)
{
int x = vec[p.to][i].to;
if(!visited[x] && d[x] > d[p.to] + vec[p.to][i].cost)
{
d[x] = d[p.to] + vec[p.to][i].cost;
q.push(Node(x,d[x]));
}
}
}
if(cnt != n)
{
ans = -1;
}
}
int main()
{
int ncase;
cin>>ncase;
while(ncase--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++)
{
scanf("%d",w + i);
}
for(int i = 0;i < m;i++)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
vec[u].push_back(Node(v,c));
vec[v].push_back(Node(u,c));
}
if(n == 0 || m == 0)
{
printf("0\n");
continue;
}
djistra(n);
if(ans != -1)
{
printf("%I64d\n",ans);
}
else
{
printf("No Answer\n");
}
for(int i = 1;i <= n;i++)
{
vec[i].clear();
}
}
return 0;
}
Christmas is coming to KCM city. Suby the loyal civilian in KCM city is preparing a big neat Christmas tree. The simple structure of the tree is shown in right picture.
The tree can be represented as a collection of numbered nodes and some edges. The nodes are numbered 1 through
n. The root is always numbered 1. Every node in the tree has its weight. The weights can be different from each other. Also the shape of every available edge between two nodes is different, so the unit price of each edge is different. Because of a
technical difficulty, price of an edge will be (sum of weights of all descendant nodes) × (unit price of the edge).
Suby wants to minimize the cost of whole tree among all possible choices. Also he wants to use all nodes because he wants a large tree. So he decided to ask you for helping solve this task by find the minimum cost.
Input
The input consists of T test cases. The number of test cases T is given in the first line of the input file. Each test case consists of several lines. Two numbers
v, e (0 ≤ v, e ≤ 50000) are given in the first line of each test case. On the next line,
v positive integers wi indicating the weights of
v nodes are given in one line. On the following e lines, each line contain three positive integers
a, b, c indicating the edge which is able to connect two nodes
a and b, and unit price c.
All numbers in input are less than 216.
Output
For each test case, output an integer indicating the minimum possible cost for the tree in one line. If there is no way to build a Christmas tree, print “No Answer” in one line.
Sample Input
2 2 1 1 1 1 2 15 7 7 200 10 20 30 40 50 60 1 2 1 2 3 3 2 4 2 3 5 4 3 7 2 3 6 3 1 5 9
Sample Output
15 1210
题解:其实就是求最短路径,反正我是没读懂,看来别人的感觉和题目说的完全不同。
SPFA(数组邻接表):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const long long INF = 0x3ffffffffffff;
struct Node
{
int to;
int cost;
Node(){
}
Node(int a,int b)
{
to = a;
cost = b;
}
};
int first[50005];
int next[100005];
Node e[100005];
bool visited[50005];
long long d[50005];
long long w[50005];
void spfa(int n)
{
memset(visited,false,sizeof(visited));
for(int i = 1;i <= n;i++)
{
d[i] = INF;
}
d[1] = 0;
queue<int> q;
q.push(1);
visited[1] = true;
while(!q.empty())
{
int x = q.front();
q.pop();
visited[x] = false;
for(int i = first[x];i != -1;i = next[i])
{
int t = e[i].to;
if(d[t] > d[x] + e[i].cost)
{
d[t] = d[x] + e[i].cost;
if(!visited[t])
{
q.push(t);
visited[t] = true;
}
}
}
}
}
int main()
{
int ncase;
cin>>ncase;
while(ncase--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++)
{
first[i] = -1;
scanf("%lld",w + i);
}
int k = 0;
for(int i = 0;i < m;i++)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
e[k].to = v;
e[k].cost = c;
next[k] = first[u];
first[u] = k++;
e[k].to = u;
e[k].cost = c;
next[k] = first[v];
first[v] = k++;
}
if(n == 0 || m == 0)
{
printf("0\n");
continue;
}
spfa(n);
bool flag = true;
long long ans = 0;
for(int i = 1;i <= n;i++)
{
if(d[i] == INF)
{
flag = false;
break;
}
ans += d[i] * w[i];
}
if(flag)
{
printf("%lld\n",ans);
}
else
{
printf("No Answer\n");
}
}
return 0;
}
djistra(数组邻接表):
#i
c5cd
nclude <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const __int64 INF = 10000000000;
struct Node
{
int to;
__int64 cost;
Node(){
}
Node(int a,__int64 b)
{
to = a;
cost = b;
}
bool operator< (Node t) const
{
return cost > t.cost;
}
};
int first[50005];
int next[100005];
Node e[100005];
bool visited[50005];
__int64 d[50005];
__int64 w[50005];
__int64 ans = 0;
void djistra(int n)
{
memset(visited,false,sizeof(visited));
for(int i = 1;i <= n;i++)
{
d[i] = INF;
}
d[1] = 0;
priority_queue<Node> q;
q.push(Node(1,0));
ans = 0;
int cnt = 0;
while(!q.empty())
{
Node p = q.top();
q.pop();
if(visited[p.to])
{
continue;
}
cnt++;
ans += d[p.to] * w[p.to];
visited[p.to] = true;
for(int i = first[p.to];i != -1;i = next[i])
{
int x = e[i].to;
if(!visited[x] && d[x] > d[p.to] + e[i].cost)
{
d[x] = d[p.to] + e[i].cost;
q.push(Node(x,d[x]));
}
}
}
if(cnt != n)
{
ans = -1;
}
}
int main()
{
int ncase;
cin>>ncase;
while(ncase--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++)
{
first[i] = -1;
scanf("%I64d",w + i);
}
int k = 0;
for(int i = 0;i < m;i++)
{
int u,v;
__int64 c;
scanf("%d%d%I64d",&u,&v,&c);
e[k].to = v;
e[k].cost = c;
next[k] = first[u];
first[u] = k++;
e[k].to = u;
e[k].cost = c;
next[k] = first[v];
first[v] = k++;
}
if(n == 0 || n == 1)
{
printf("0\n");
continue;
}
djistra(n);
if(ans != -1)
{
printf("%I64d\n",ans);
}
else
{
printf("No Answer\n");
}
}
return 0;
}
超时vector:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <utility>
using namespace std;
const __int64 INF = 0x3fffffffffff;
struct Node
{
int to;
int cost;
Node(){
}
Node(int a,int b)
{
to = a;
cost = b;
}
bool operator< (Node t) const
{
return cost > t.cost;
}
};
typedef pair<__int64,int>plli;
priority_queue<plli , vector<plli> , greater<plli> > q;
vector<Node> vec[50005];
bool visited[50005];
__int64 d[50005];
int w[50005];
__int64 ans;
void djistra(int n)
{
memset(visited,false,sizeof(visited));
for(int i = 1;i <= n;i++)
{
d[i] = INF;
}
d[1] = 0;
ans = 0;
priority_queue<Node> q;
q.push(Node(1,0));
int cnt = 0;
while(!q.empty())
{
Node p = q.top();
q.pop();
if(visited[p.to])
{
continue;
}
ans += w[p.to] * d[p.to];
cnt++;
visited[p.to] = true;
for(int i = 0;i < vec[p.to].size();i++)
{
int x = vec[p.to][i].to;
if(!visited[x] && d[x] > d[p.to] + vec[p.to][i].cost)
{
d[x] = d[p.to] + vec[p.to][i].cost;
q.push(Node(x,d[x]));
}
}
}
if(cnt != n)
{
ans = -1;
}
}
int main()
{
int ncase;
cin>>ncase;
while(ncase--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++)
{
scanf("%d",w + i);
}
for(int i = 0;i < m;i++)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
vec[u].push_back(Node(v,c));
vec[v].push_back(Node(u,c));
}
if(n == 0 || m == 0)
{
printf("0\n");
continue;
}
djistra(n);
if(ans != -1)
{
printf("%I64d\n",ans);
}
else
{
printf("No Answer\n");
}
for(int i = 1;i <= n;i++)
{
vec[i].clear();
}
}
return 0;
}
相关文章推荐
- Android 点击空白处隐藏键盘
- load-on-startup 解释
- struts note1
- ACM_阶段性总结 ACM_动态规划(DP)
- IntelliJ IDEA 工具的常见使用
- MySQL ODBC创建
- Redis在windows下的安装使用
- ZOJ3827——Information Entropy
- android开发工具
- 堆排序
- 求一个整数的所有位相加之和
- Android自定义布局
- apache的https访问配置
- ToggleButton
- 线程初体验之跑马灯
- 分类与回归区别是什么?
- A Dicey Problem (Uva 810 & poj 1872 bfs)
- 阅读方法
- 线段树(求矩形并,交,并减交的面积)
- UML类图实例