Sending email - UVa 10986 Dijkstra+优先队列优化

76 篇文章 0 订阅
Sending email
Time Limit: 3 seconds

"A new internet watchdog is creating a stir in
Springfield. Mr. X, if that is his real name, has
come up with a sensational scoop."

Kent Brockman

There are n SMTP servers connected by network cables. Each of the m cables connects two computers and has a certain latency measured in milliseconds required to send an email message. What is the shortest time required to send a message from server S to server T along a sequence of cables? Assume that there is no delay incurred at any of the servers.

Input
The first line of input gives the number of cases, NN test cases follow. Each one starts with a line containing n (2<=n<20000), m (0<=m<50000), S (0<=S<n) and T (0<=T<n). S!=T. The next m lines will each contain 3 integers: 2 different servers (in the range [0, n-1]) that are connected by a bidirectional cable and the latency, w, along this cable (0<=w<=10000).

Output
For each test case, output the line "Case #x:" followed by the number of milliseconds required to send a message from S to T. Print "unreachable" if there is no route from S to T.

Sample InputSample Output
3
2 1 0 1
0 1 100
3 3 2 0
0 1 100
0 2 200
1 2 50
2 0 0 1
Case #1: 100
Case #2: 150
Case #3: unreachable


题意:从S到T的最短路径。
思路:裸Dijkstra,不过因为数的范围太大,要用链表来存边,还要用优先队列优化。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
    int v,len;
    node *next;
}*Head[20010],tree[100010];
struct node2
{
    int val,pos;
    bool operator<(const node2 A)const
    {
        return A.val<val;
    }
};
int T,t,ptr,n,m,S,E,f[20010],INF=1e9,vis[20010];
priority_queue<node2> qu;
void AddEdge(int a,int b,int c)
{
    tree[ptr].v=b;
    tree[ptr].len=c;
    tree[ptr].next=Head[a];
    Head[a]=&tree[ptr++];
}
void Dijkstra()
{
    int i,j,k,ret,pos,v,len;
    for(i=0;i<n;i++)
       f[i]=INF;
    f[S]=0;
    node *p;
    node2 A;
    while(!qu.empty())
      qu.pop();
    A.val=0;
    A.pos=S;
    qu.push(A);
    for(i=1;i<=n;i++)
    {
        ret=INF;
        while(true)
        {
            if(qu.empty())
              break;
            A=qu.top();
            if(vis[A.pos]==t)
              qu.pop();
            else
            {
                pos=A.pos;
                break;
            }
        }
        if(qu.empty())
          break;
        vis[pos]=t;
        p=Head[pos];
        while(p)
        {
            v=p->v;
            len=p->len;
            if(f[pos]+len<f[v])
            {
                A.val=f[pos]+len;
                A.pos=v;
                qu.push(A);
                f[v]=f[pos]+len;
            }
            p=p->next;
        }
    }
}
int main()
{
    int i,j,k,u,v,w;
    scanf("%d",&T);
    for(t=1;t<=T;t++)
    {
        scanf("%d%d%d%d",&n,&m,&S,&E);
        memset(Head,NULL,sizeof(Head));
        ptr=0;
        for(i=1;i<=m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            AddEdge(u,v,w);
            AddEdge(v,u,w);
        }
        Dijkstra();
        if(f[E]>=INF)
          printf("Case #%d: unreachable\n",t);
        else
          printf("Case #%d: %d\n",t,f[E]);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用 Python 的 smtplib 模块来发送电子邮件。首先,需要准备好要发送的电子邮件的内容,包括发件人地址、收件人地址、主题和正文。然后,使用 smtplib 模块的 SMTP 类来连接到 SMTP 服务器,并使用 login() 方法登录。接着,使用 sendmail() 方法来发送电子邮件。最后,使用 quit() 方法来断开与 SMTP 服务器的连接。 例如,下面是一段示例代码,展示了如何使用 Python 发送一封简单的电子邮件: ```python import smtplib # 要发送的电子邮件内容 from_addr = 'sender@example.com' to_addr = 'receiver@example.com' subject = 'Test Email' body = 'This is a test email sent from Python.' # 连接到 SMTP 服务器 smtp_server = smtplib.SMTP('smtp.example.com') # 登录到 SMTP 服务器 smtp_server.login('username', 'password') # 构造电子邮件 msg = f'Subject: {subject}\n\n{body}' # 发送电子邮件 smtp_server.sendmail(from_addr, to_addr, msg) # 断开与 SMTP 服务器的连接 smtp_server.quit() ``` 注意,在上面的代码中,需要替换 `sender@example.com` 和 `receiver@example.com` 为实际的发件人地址和收件人地址,并替换 `smtp.example.com` 为实际的 SMTP 服务器地址。此外,需要替换 `username` 和 `password` 为登录 SMTP 服务器的用 ### 回答2: 使用Python发送电子邮件,你可以使用SMTP(简单邮件传输协议)库。 首先,你需要导入smtplib库,以便与SMTP服务器进行通信。然后,创建一个SMTP对象,并登录到你的电子邮件帐户。你需要提供SMTP服务器的主机名和端口号,以及你的电子邮件地址和密码。 接下来,你可以通过调用sendmail()方法来发送邮件。你需要提供发件人,收件人和邮件内容。邮件内容可以通过构建一个MIMEText对象来实现。 以下是一个简单的示例代码,演示如何向一个收件人发送电子邮件: ```python import smtplib from email.mime.text import MIMEText def send_email(): sender = 'example@example.com' receiver = 'example2@example.com' subject = 'Hello from Python' body = 'This is a test email.' msg = MIMEText(body) msg['Subject'] = subject msg['From'] = sender msg['To'] = receiver smtp_server = 'smtp.example.com' smtp_port = 587 username = 'your_username' password = 'your_password' server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() server.login(username, password) server.sendmail(sender, receiver, msg.as_string()) server.quit() send_email() ``` 在上面的示例中,你需要将示例电子邮件地址,SMTP服务器和登录凭据替换为你自己的信息。 这只是一个简单的示例,你可以根据自己的需求进行进一步的定制,比如添加附件、使用HTML格式的邮件等。 总之,使用Python发送电子邮件是非常简单的,只需要使用Python的smtplib库和MIMEText对象即可实现。 ### 回答3: 使用Python发送电子邮件非常简便和方便。下面是一个基本的步骤。 首先,需要导入smtplib和email模块,分别用于发送邮件和构造邮件。可以使用以下代码导入这些模块: ``` import smtplib from email.mime.text import MIMEText ``` 然后,需要配置SMTP服务器的信息,包括SMTP服务器地址、端口号和认证信息。例如,如果使用Gmail作为SMTP服务器,可以使用以下代码进行配置: ``` smtp_server = 'smtp.gmail.com' smtp_port = 587 username = 'your_email@gmail.com' password = 'your_password' ``` 接下来,可以构造邮件内容。例如,可以使用MIMEText对象创建一个简单的纯文本邮件: ``` message = MIMEText('This is the email content.', 'plain') message['From'] = 'sender@example.com' message['To'] = 'receiver@example.com' message['Subject'] = 'Email Subject' ``` 如果要发送HTML格式的邮件,可以将'MIMEText'改为'MIMEText(html_content, 'html')',其中'html_content'是包含HTML内容的字符串。 最后,使用smtplib模块发送邮件。可以使用以下代码发送邮件: ``` with smtplib.SMTP(smtp_server, smtp_port) as server: server.starttls() server.login(username, password) server.send_message(message) ``` 在这个例子中,首先使用starttls()方法启用TLS加密连接,然后使用login()方法进行身份验证,并最后使用send_message()方法发送邮件。 以上就是使用Python发送电子邮件的基本步骤。根据需要,还可以添加更多的功能,例如添加附件或发送多个收件人。参考Python文档以了解更多细节和选项。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值