最短路,dijkstra算法。

Description

Earth Hour is an annual international event created by the WWF (World Wide Fund for Nature/World Wildlife Fund), held on the last Saturday of March, that asks households and businesses to turn off their non-essential lights and electrical appliances for one hour to raise awareness towards the need to take action on climate change. 
To respond to the event of this year, the manager of Hunan University campus decides to turn off some street lights at night. Each street light can be viewed as a point in a plane, which casts flash in a circular area with certain radius.
What's more, if two illuminated circles share one intersection or a point, they can be regarded as connected.
Now the manager wants to turn off as many lights as possible, guaranteeing that the illuminated area of the library, the study room and the dormitory are still connected(directly or indirectly). So, at least the lights in these three places will not be turned off.
 

Input

The first line contains a single integer T, which tells you there are T cases followed.
In each case:
The first line is an integer N( 3<=N<=200 ), means there are N street lights at total.
Then there are N lines: each line contain 3 integers, X,Y,R,( 1<=X,Y,R<=1000 ), means the light in position(X,Y) can illuminate a circle area with the radius of R. Note that the 1st of the N lines is corresponding to the library, the 2nd line is corresponding to the study room, and the 3rd line is corresponding to the dorm.
 

Output

One case per line, output the maximal number of lights that can be turned off.
Note that if none of the lights is turned off and the three places are still not connected. Just output -1.
 

Sample Input

    
    
3 5 1 1 1 1 4 1 4 1 1 2 2 1 3 3 1 7 1 1 1 4 1 1 2 4 1 1 3 1 3 1 1 3 3 1 4 3 1 6 1 1 1 5 1 1 5 5 1 3 1 2 5 3 2 3 3 1
 

Sample Output

    
    
-1 2 1
题目大意; 题目大意,有n个灯,编号为1到n,给出每个灯的坐标和灯光照的范围,要求,1号2号3号必须开着,求让它们3个连在一起的情况下可以关闭的最多的灯是多少?

题意也转化一下为要用灯将1和2和3连接在一起,并且要最少的灯将他们连接在一起。

这道题就是找出一个点作为桥梁,让目标三个点到该点的最短路之和最小,就是需要最小的值把1 2 3 连接在一起,包括(1,2,3)3个点,用n减去得到结果。

一开始看题看醉了,根本不知道该干嘛。后来搜了下题解才知道。。。。。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
const int N = 210;
int n,map[N][N],vis[N];
struct node
{
    double x,y;
    double r;
} a[N];
int G(int x,int y)
{
    double len=sqrt((a[x].x-a[y].x)*(a[x].x-a[y].x)+(a[x].y-a[y].y)*(a[x].y-a[y].y));
    if(len<=a[x].r+a[y].r)
        return 1;
    else
        return 0;
}
void DJ(int x,int d[])
{
    int i,j;
    memset(vis,0,sizeof(vis));
    for(i=0; i<N; i++)
        d[i]=INF;
    d[x]=0;
    vis[x]=1;
    for(i=0; i<n; i++)
    {
        int temp=INF;
        for(j=0; j<n; j++)
            if(!vis[j]&&d[j]<temp)
                temp=d[x=j];
        vis[x]=1;
        for(j=0; j<n; j++)
            if(!vis[j]&&map[x][j]&&d[x]+map[x][j]<d[j])
                d[j]=d[x]+map[x][j];

    }
}
int main()
{
    int T,d1[N],d2[N],d3[N];
    scanf("%d",&T);
    while(T--)
    {
        memset(map,0,sizeof(map));
        memset(d1,0,sizeof(d1));
        memset(d2,0,sizeof(d2));
        memset(d3,0,sizeof(d3));
        int i,j;
        scanf("%d",&n);
        for(i=0; i<n; i++)
            scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].r);
        for(i=0; i<n; i++)
            for(j=i+1; j<n; j++)
            {
                if(G(i,j))
                    map[i][j]=map[j][i]=1;
            }
        DJ(0,d1);
        DJ(1,d2);
        DJ(2,d3);
        int mm=INF,sum=0;
        for(i=0; i<n; i++)
        {
            if(d1[i]!=INF&&d2[i]!=INF&&d3[i]!=INF)//这个和3个点都能连通
            {
                sum=d1[i]+d2[i]+d3[i];//此点到3个点的最短路之和
                if(sum<mm)
                    mm=sum;
            }
        }
        if(mm==INF)
            printf("-1\n");
        else
            printf("%d\n",n-mm-1);
    }
}

当然可以!以下是使用Dijkstra算法求解最短路径的MATLAB代码示例: ```matlab function [dist, path] = dijkstra(adjMatrix, startNode) numNodes = size(adjMatrix, 1); % 初始化距离数组 dist = inf(1, numNodes); dist(startNode) = 0; % 初始化路径数组 path = cell(1, numNodes); path{startNode} = startNode; % 创建一个标记数组,用于记录节点是否已被访问 visited = false(1, numNodes); % 运行Dijkstra算法 for i = 1:numNodes-1 % 找到当前距离最小的节点 [~, currentNode] = min(dist .* ~visited); visited(currentNode) = true; % 更新与当前节点相邻的节点的距离 neighbors = find(adjMatrix(currentNode, :)); for j = neighbors if ~visited(j) && dist(currentNode) + adjMatrix(currentNode, j) < dist(j) dist(j) = dist(currentNode) + adjMatrix(currentNode, j); path{j} = [path{currentNode}, j]; end end end end ``` 使用示例: ```matlab % 定义一个邻接矩阵表示图的连接关系 adjMatrix = [ 0 3 0 4 0; 3 0 5 0 0; 0 5 0 2 6; 4 0 2 0 1; 0 0 6 1 0 ]; % 调用Dijkstra算法求解最短路径 [startNode, endNode] = deal(1, 5); [dist, path] = dijkstra(adjMatrix, startNode); % 输出结果 disp(['从节点', num2str(startNode), '到节点', num2str(endNode), '的最短距离为:', num2str(dist(endNode))]); disp(['最短路径为:', num2str(path{endNode})]); ``` 这段代码实现了Dijkstra算法的逻辑,并且可以根据输入的邻接矩阵和起始节点,求解出最短路径的距离和路径。希望能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值