uva10986Sending email (SPFA)

题意:给定一个起点和终点,问最短路径

思路:最短路径测试模板题...


#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 20005
#define LL long long
int cas=1,T;
int n,m,w;
const int INF = 1<<29;
struct Edge
{
	int from,to,dist;
	Edge(int u,int v,int d):from(u),to(v),dist(d){}
};
struct Bellman_ford
{
	vector<Edge>edges;
	vector<int> G[maxn];
	int d[maxn];
    int inq[maxn];
	int cnt[maxn];
	void init()
	{
       for (int i = 0;i<=n;i++)
		   G[i].clear();
	   edges.clear();
	}
	void AddEdge(int from,int to,int dist)
	{
       edges.push_back(Edge(from,to,dist));
	   int mm = edges.size();
	   G[from].push_back(mm-1);
	}
	bool bellman_ford(int s)
	{
		queue<int>q;
		memset(inq,0,sizeof(inq));
		memset(cnt,0,sizeof(cnt));
		for (int i =0;i<=n;i++)
		   d[i]=INF;
		d[s]=0;
		inq[s]=1;
		q.push(s);
		while (!q.empty())
		{
			int u = q.front();q.pop();
			inq[u] = false;
			for (int i =0;i<G[u].size();i++)
			{
				Edge &e = edges[G[u][i]];
				if (d[u]<INF && d[e.to]>d[u]+e.dist)
				{
					d[e.to] = d[u] + e.dist;
					if (!inq[e.to])
					{
						q.push(e.to);
						inq[e.to]=1;
						if (++cnt[e.to]>n)
							return false;
					}
				}
			}
		}
		return true;
	}
};
int main()
{
	//freopen("in","r",stdin);
	scanf("%d",&T);
	while (T--)
	{
		int s,e;
        scanf("%d%d%d%d",&n,&m,&s,&e);
		Bellman_ford bf;
		bf.init();
		for (int i = 0;i<m;i++)
		{
			int u,v,t;
			scanf("%d%d%d",&u,&v,&t);
            bf.AddEdge(u,v,t);
			bf.AddEdge(v,u,t);
		}
		
		bf.bellman_ford(s);
		if (bf.d[e] ==INF)
			printf("Case #%d: unreachable\n",cas++);
		else
			printf("Case #%d: %d\n",cas++,bf.d[e]);
	}
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}



题目

“A new internet watchdog is creating a stir inSpringfield. Mr. X, if that is his real name, hascome up with a sensational scoop.”Kent BrockmanThere are n SMTP servers connected by network cables. Each of the m cables connects two computersand has a certain latency measured in milliseconds required to send an email message. Whatis 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, N. N test cases follow. Each one starts with a linecontaining n (2 ≤ n ≤ 20000), m (0 ≤ m ≤ 50000), S (0 ≤ S < n) and T (0 ≤ T < n). S ̸= T. Thenext m lines will each contain 3 integers: 2 different servers (in the range [0, n − 1]) that are connectedby 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 senda message from S to T. Print ‘unreachable’ if there is no route from S to T.

Sample Input

32 1 0 10 1 1003 3 2 00 1 1000 2 2001 2 502 0 0 1

Sample Output

Case #1: 100

Case #2: 150

Case #3: unreachable

  • 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、付费专栏及课程。

余额充值